开发者

How to separate word in php?

How to separate word in php ? Example i have one variable str = "a1,c1,d1;a2,c2,d2;a3,c3,d3"; I want to separate str in to the following formats: tt1=a1,c1,d1, tt2=a2,c2,d2, tt3=a3,c3,d3 How to do like this in php?

it try this but not correct:

        $str = "a1,c1,d1;a2,c2,d2;a3,c3,d3";
    $strSplit = explode(';', $str);
    $final = array();    

    for($i=0, $j=0; $i<count($strSplit); $i++, $j++)
    {
        $final[$j] = $strSplit[$i]开发者_开发技巧 . ' ' . $strSplit[$i+1];
    }


Does simply

$str = "a1,c1,d1;a2,c2,d2;a3,c3,d3";
$arr = explode(';',$str);
var_dump($arr);

...not give you what you want??

Or do you want the parts as variables in the their own right, starting with 'tt'?

$str = "a1,c1,d1;a2,c2,d2;a3,c3,d3";
$arr = explode(';',$str);
for ($i = 0; isset($arr[$i]); $i++) ${'tt'.($i + 1)} = $arr[$i];
print "tt1=$tt1 tt2=$tt2 tt3=$tt3"

Or possibly as an array of arrays?

$str = "a1,c1,d1;a2,c2,d2;a3,c3,d3";
$arr = explode(';',$str);
for ($i = 0; isset($arr[$i]); $i++) $arr[$i] = explode(',',$arr[$i]);
var_dump($arr);

Or do you want to replace the semi-colon (;) characters with spaces?

$str = "a1,c1,d1;a2,c2,d2;a3,c3,d3";
$out = str_replace(';',' ',$str);
print $out;


$str = "a1,c1,d1;a2,c2,d2;a3,c3,d3";
$tt = explode(";", $str); //your answers: $tt[0], $tt[1] and $tt[2]

if you want to join them again with a space between them, you can try:

$final = implode(" ", $tt);


You can use the php function strtok:

http://php.net/manual/en/function.strtok.php

and use the semicolon as a token:

$array = strtok($string, ';');

this will create an array and each entry will be the part of the original string until the semicolon

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜