Question

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

Was it helpful?

Solution

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

OTHER TIPS

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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top