I have pulled a string out of a database it contains some html code as for example:
something about thing one<br>
now comes the second thing<br>
we're not done yet, here's another one<br>
and last but not least is the fourth one<br>
So I have four lines but when I print out the string I get an output like in the example above. What'd I'd like to do is manipulate every line so that I'd be able to do this:
<span>something about thing one</span>
<span>now comes the second thing</span>
<span>we're not done yet, here's another one</span>
<span>and last but not least is the fourth one</span>
I'd also like to have a counter for how many lines are there in a string (like for this one there are 4 lines) so I can set "odd" and "even" class for the span.
How do I do this?
Use
explode
to split and I would preferfor
loop in these scenario instead offoreach
as the latter will return an emptyspan
tags in the end since it loops five times.To get the count you can use
count
function:Simply use the explode() function with
PHP_EOL
constant as delimiter:After you can iterate the returned array to parse lines, for example:
You can explode the string with the delimiter and then use a foreach loop to get the answer you want.
//Explode the input string with br as delimiter
//Filter $data array to remove any empty or null values
//to get total data count
//now use loop to what you want
Hope this helps