I have this kind of string :
$string = "<strong>Blabla1</strong> Blaabla2<br /> Blaabla3 <strong>Blaabla4</strong> Blaabla5 Blaabla6<br /><br /> Blaabla7 <span style='color:#B22222;'>Blaabla8</span> Blaabla9";
I'm trying to explode each word where there is a " "
or "<br />"
with preg_split
.
My conditions :
For each word (Blablax
), I need to keep his tags like <strong>
, <span>
, <em>
... but split him after a <br />
or more <br />
I tried this, thanks to another post on stackoverflow :
preg_split('/<br(\s\/)?>\K|\s/',$string,null,PREG_SPLIT_NO_EMPTY);
OUTPUT:
array (size=12)
0 => string '<strong>Blabla1</strong>' (length=24)
1 => string 'Blaabla2<br />' (length=14)
2 => string 'Blaabla3' (length=8)
3 => string '<strong>Blaabla4</strong>' (length=25)
4 => string 'Blaabla5' (length=8)
5 => string 'Blaabla6<br />' (length=14)
6 => string '<br' (length=3)
7 => string '/>' (length=2)
8 => string 'Blaabla7' (length=8)
9 => string '<span' (length=5)
10 => string 'style='color:#B22222;'>Blaabla8</span>' (length=38)
11 => string 'Blaabla9' (length=8)
Everything works except for index 6
and index 7
(see above in OUTPUT) and index 9
and index 10
What I'll exepect :
array (size=12)
0 => string '<strong>Blabla1</strong>' (length=24)
1 => string 'Blaabla2<br />' (length=14)
2 => string 'Blaabla3' (length=8)
3 => string '<strong>Blaabla4</strong>' (length=25)
4 => string 'Blaabla5' (length=8)
5 => string 'Blaabla6<br /><br />' (length=14)
6 => string 'Blaabla7' (length=8)
7 => string '<span style='color:#B22222;'>Blaabla8</span>' (length=45)
8 => string 'Blaabla9' (length=8)
See index 5
and index 7
My regex works if I have just one <br />
but if more than one, there is a mistakes... idem if I have a <span style...>
Thanks !
\K|\s/g',$string,null,PREG_SPLIT_NO_EMPTY); ` – Noman Apr 16 '15 at 10:28
` at index 1? It does not present in the input. – nhahtdh Apr 16 '15 at 10:37
` at the index 1... – Zagloo Apr 16 '15 at 10:39