Split <b> in php

For this code, I need to take a line like

<b>hey</b> more text<b>hey2</b> other text

      

and I expect the returned array to look like

<b>hey</b> more text
<b>hey2</b> other text

      

However, it is not. How to make it look like above? my test code

$myArr = split("<b>", "<b>hey</b> more text<b>hey2</b> other text");
foreach($myArr as $e)
{
    echo "e = $e\n-------------\n";
}

      

output

e =
-------------
e = hey</b> more text
-------------
e = hey2</b> other text

      

I need b> to stay. How do I remove the first empty array?

+2


source to share


4 answers


the "split" function is deprecated, it is best not to use it.



Instead, use "explode" on the <b> and then add back to each element and pop the first element out of the array.

+1


source


echo "e = <b>$e\n-------------\n";

      

You need to add <b> because split removes it from the string.



As for empty elements of the try array:

$array = array_filter($array); 

      

+4


source


you can use it as it is and add $ e = "". $ e; before the echo

0


source


You can add <b>

after splitting and just the pop()

first variable in the array.

0


source







All Articles