If there is not more HTML, it's okay to use RegEx. Otherwise there are many better ways.
Use <br(\s\/)?>\K|\s
:
$matches = preg_split('/<br(\s\/)?>\K|\s/',$string);
This will also work for <br>
(which is correct HTML too)
Consider the flag PREG_SPLIT_NO_EMPTY
, because there are going to be empty elements using your example string:
preg_split('/<br(\s\/)?>\K|\s/',$string,null,PREG_SPLIT_NO_EMPTY);
Update: To keep the <br />
, you need to reset the match using \K
. There is a good example on this in the language reference:
\K can be used to reset the match start since PHP 5.2.4. For example,
the pattern foo\Kbar matches "foobar", but reports that it has matched
"bar". The use of \K does not interfere with the setting of captured
substrings. For example, when the pattern (foo)\Kbar matches "foobar",
the first substring is still set to "foo".
`? Your expected output shows it retained ... – hwnd Apr 15 '15 at 17:17
` to spaces, and then did the split on spaces. You don't need to do everything in one single regex. – Andy Lester Apr 15 '15 at 17:45
:) – Zagloo Apr 16 '15 at 06:49