0

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

ericlee
  • 2,703
  • 11
  • 43
  • 68

2 Answers2

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

vee
  • 38,255
  • 7
  • 74
  • 78
0

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));
Community
  • 1
  • 1
Curtis Mattoon
  • 4,642
  • 2
  • 27
  • 34