例如
$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

解决方案 »

  1.   

    $a="a good boy,a bag,and a good gril,a tree,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
            )
     
    )
      

  2.   

    我用split($a,$b)分割,然后再FOR判断,你这个用数组和正则也不错,感谢,平时不敢用正则,怕出错
      

  3.   

    正则能不用正则则不用,正则的效率差$arr = array_filter(explode(',',$a),create_function('$a','return strpos($a,"good") !== false;'));print_r($arr);