Ho questo tipo di string:
$string = "<strong>Blabla1</strong> Blaabla2<br /> Blaabla3 <strong>Blaabla4</strong> Blaabla5 Blaabla6<br /><br /> Blaabla7 <span style='color:#B22222;'>Blaabla8</span> Blaabla9";
Sto cercando di far esplodere each parola where c'è un " "
o "<br />"
con preg_split
.
Le mie condizioni:
Per each parola ( Blablax
), ho bisogno di mantenere i suoi tag come <strong>
, <span>
, <em>
… ma dividilo dopo un <br />
o più <br />
Ho provato questo, grazie a un altro post su StackOverflow:
preg_split('/<br(\s\/)?>\K|\s/',$string,null,PREG_SPLIT_NO_EMPTY);
PRODUZIONE:
arrays (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)
Tutto funziona tranne che per l' index 6
e l' index 7
(vedi sopra in OUTPUT) e l' index 9
e l' index 10
Che cosa esiterò:
arrays (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)
Vedi index 5
e index 7
Il mio regex funziona se ne ho uno solo <br />
ma se più di uno, ci sono errori … idem se ho uno <span style...>
Grazie !
$string = "<strong>Blabla1</strong> Blaabla2<br /> Blaabla3 <strong>Blaabla4</strong> Blaabla5 Blaabla6<br /><br /> Blaabla7 <span style='color:#B22222;'>Blaabla8</span> Blaabla9"; $matches = preg_split('/(<br.*?>|<span.*>)+\K|\s/sim', $string, null, PREG_SPLIT_NO_EMPTY ); var_dump($matches); /* arrays(9) { [0]=> string(24) "<strong>Blabla1</strong>" [1]=> string(14) "Blaabla2<br />" [2]=> string(8) "Blaabla3" [3]=> string(25) "<strong>Blaabla4</strong>" [4]=> string(8) "Blaabla5" [5]=> string(20) "Blaabla6<br /><br />" [6]=> string(8) "Blaabla7" [7]=> string(44) "<span style='color:#B22222;'>Blaabla8</span>" [8]=> string(8) "Blaabla9" } */
DEMO
Guardando il tuo arrays previsto a indice 5 e indice 7, probabilmente vuoi questo regex:
preg_split('~(?:</?[a-zA-Z0-9][^>]*+>|\S)++\K|\s~',$string,null,PREG_SPLIT_NO_EMPTY);
Demo su ideone
Produzione:
arrays(9) { [0]=> string(24) "<strong>Blabla1</strong>" [1]=> string(14) "Blaabla2<br />" [2]=> string(8) "Blaabla3" [3]=> string(25) "<strong>Blaabla4</strong>" [4]=> string(8) "Blaabla5" [5]=> string(20) "Blaabla6<br /><br />" [6]=> string(8) "Blaabla7" [7]=> string(44) "<span style='color:#B22222;'>Blaabla8</span>" [8]=> string(8) "Blaabla9" }
L'espressione regolare tenta di associare un tag completo e, se non è ansible consumare un tag completo, consumerà un carattere non spaziale, quindi risciacqua e ripeti. Ciò eviterà che i tag vengano suddivisi, il che fornisce l'output previsto per l'indice 5 e 7.
Non raccomanderei di farlo con regex, comunque. Non ho consultato le specifiche HTML durante la scrittura della regex, quindi la regex è molto fragile e potrebbe interrompersi in input in the wild. Potresti voler imparare come analizzare correttamente l'HTML con una delle librerie elencate in questa domanda: Come analizzi ed elabora HTML / XML in PHP?
Ecco la regex
((?:<br\s*\/?>)+)|(?<!<br)\s+(?!\/?>)
Usalo con preg_replace
usando $1\n
come string sostitutiva, e poi puoi dividere per newline per get l'arrays (rimuovendo quelli vuoti).
Guarda la demo .