0

I have the following preg_split which works fine to split a string based on spaces and angle brackets except the result array doesn't contain < and > brackets as it should. It's putting empty strings where the angle brackets are supposed to be.

$str = 'yo <img src="" />';
$strarr = [];
$strarr = preg_split("/([\s<>])/",$str);  //split based on < and > and space
var_dump($strarr);

This gives the following output in PHP:

array(6) {
  [0]=> string(2) "yo" 
  [1]=> string(0) ""
  [2]=> string(3) "img"
  [3]=> string(6) "src="""
  [4]=> string(1) "/"
  [5]=> string(0) ""
}

Also I'm not trying to parse html, so please refrain from suggesting DOM. Thanks!

P.S : The regex works fine in javascript str.split although that also returns some extra empty elements (incase anyone knows why) yet it does contain all the items including the angle brackets in javascript. How do I get the angle brackets to show up in the result string in PHP?

xmxmxmx
  • 409
  • 1
  • 6
  • 16

0 Answers0