问题
I've some text that i wish to parse
$str = "text1<br/>text2<br/>text3
I've tried using
print_r( preg_split("<br/>", $str));
but it is not giving me the desired output
回答1:
Try the following:
$str = "text1<br/>text2<br/>text3";
print_r(preg_split("/<br\/>/", $str));
I'm assuming missing the closing quote "
at the end of the $str = "text1<br/>text2<br/>text3"
is just a typo.
Take a look at this page on how to specify the string $pattern
parameter: http://php.net/manual/en/function.preg-split.php
回答2:
It's because you're not using the correct regular expression. Is there a reason you can't use explode()? Regex is problematic, overly complicated at times, and much slower. If you know you'll always be splitting at the BR tag, explode is much more efficient.
Parsing HTML with regex is a bad idea, but here you go:
var_dump(preg_split('/(<br\ ?\/?>)+/', $str));
来源:https://stackoverflow.com/questions/17775374/php-preg-split-for-forwardslash