I want to split a string like this one :
'This <p>is</p> a <p>string</p>'
I want to get 4 strings :
This
<p>is</p>
<p>string</p>
So I want to find <p></p>
and its content one by one to split it. How can I do it keeping the same sequence ?
I can get 'This' with that code : $html1 = strstr($html, '<p', true);
but I don't know how to continue splitting and how to do it for variable strings with many needles (at least 2 different needles). Can you help me with it ?
You could use preg_split
with some options ($s
is the input string):
preg_split("#\s*(<p>.*?</p>)\s*#", $s, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
This returns an array. For your sample input it returns:
["This", "<p>is</p>", "a", "<p>string</p>"]