例如
$a="a good boy,a bag,and a good gril,a tree,but they are not good student,and not good baby";
$b="good";
$firstplace=strpos($a,$b);//得到b在a中第1次出现的位置
$secondplace=strpos($a,$b,$firstplace+1); //得到b在a中第2次出现的位置现在我想显示所有带有good的段落,截取的时候按逗号截取,不知如何写?也就是说我想得到每个good关键词前后最近的逗号,就可以了。
效果如下:a good boy
and a good gril
but they are not good student
and not good baby
$a="a good boy,a bag,and a good gril,a tree,but they are not good student,and not good baby";
$b="good";
$firstplace=strpos($a,$b);//得到b在a中第1次出现的位置
$secondplace=strpos($a,$b,$firstplace+1); //得到b在a中第2次出现的位置现在我想显示所有带有good的段落,截取的时候按逗号截取,不知如何写?也就是说我想得到每个good关键词前后最近的逗号,就可以了。
效果如下:a good boy
and a good gril
but they are not good student
and not good baby
preg_match_all("([^,]*?good[^,]+)", $a, $out);
print_r($out);
Array
(
[0] => Array
(
[0] => a good boy
[1] => and a good gril
[2] => but they are not good student
[3] => and not good baby
)
)