开发者

preg_match multiple help?

开发者 https://www.devze.com 2022-12-21 11:05 出处:网络
<?PHP $st开发者_JAVA技巧ring = \"[test]aaaaa[/[test][test]bbbb[/test][test]cccc[/test][test]ddd[/test]\";
<?PHP
    $st开发者_JAVA技巧ring = "[test]aaaaa[/[test][test]bbbb[/test][test]cccc[/test][test]ddd[/test]";
    echo $string . "<br>";
    preg_match("/\[test\].*?(\[\/test\])/i", $string, $m);
    print_r($m);
?>

how to get value aaaaa and bbbb in multiple from capture [test] and [/test] ?


preg_match_all("/\[test\](.*?)\[\/test\]/i", $string, $array);

$array[1] has what you want.


non regex way

$string = "[test]aaaaa[/test][test]bbbb[/test][test]cccc[/test][test]ddd[/test]";
$s = explode('[/test]',$string);
foreach ($s as $v){
    if( strpos( $v,"[test]" )!==FALSE ){
        $t=explode("[test]",$v);
        print $t[1]."\n";
    }
}

output

$ php test.php
aaaaa
bbbb
cccc
ddd
0

精彩评论

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