Tengo el siguiente código para agregar un contador incremental al lado de cada etiqueta BR, como un contador por línea para una canción con letra, pero no funciona. ¡Se agradece la ayuda!
<?php $lyrics = "<p>Every time when I look in the mirror <br />All these lines on my face getting clearer <br /> The past is gone <br /> And it went by, like dusk to dawn <br /> Isn't that the way? <br /> Everybody's got their dues in life to pay <br /> Yeah, I know nobody knows <br /> Where it comes and where it goes <br /> I know it's everybody's sin <br /> You got to lose to know how to win <br /></p>"; $arr=explode(PHP_EOL, $lyrics); foreach($arr as $index=>$ele) { echo $ele . $index+1 . "<br />"; } ?>Está intentando dividir la cadena $lyrics en la constante PHP_EOL (esta es una constante dependiente de la plataforma que se refiere a la línea que finaliza los usos de su sistema operativo). En su lugar, debe intentar dividir la cadena por <br /> .
Pruébelo de esta manera: use " <br /> " para separar y hacer que cada eco sea único:
$lyrics = "<p>Every time when I look in the mirror <br />All these lines on my face getting clearer <br /> The past is gone <br /> And it went by, like dusk to dawn <br /> Isn't that the way? <br /> Everybody's got their dues in life to pay <br /> Yeah, I know nobody knows <br /> Where it comes and where it goes <br /> I know it's everybody's sin <br /> You got to lose to know how to win <br /></p>"; $arr=explode("<br />", $lyrics); foreach($arr as $index=>$ele) { echo $ele; echo $index+1; echo "<br />"; }