Php preg_split for forwardslash?

五迷三道 提交于 2020-02-06 22:31:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!