tags:

views:

64

answers:

4
+2  Q: 

split <b>'s in php

For this code i need to take a string 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 doesnt. How do i make it look like the above? my test code is

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

output is

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

I need the b>'s to remain. How should i remove the first empty array?

+4  A: 
echo "e = <b>$e\n-------------\n";

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

As for the empty elements of the array try:

$array = array_filter($array);
bisko
+1 : simple, easy to write and to read
Clement Herreman
A: 

you could use it as you have it and add a $e = "".$e; before the echo

Flo
A: 

You can add the <b> after splitting, and just pop() the first variable in the array.

peirix
The pop wouldn't work in case of broken html: <b><b>asd<b><b><b>asd ;)
bisko
Well, you could always do a check `if ($array[0] == "") //popThatShit()`
peirix
Well yes, but if you try it you would see that you would get more empty values than just [0] :)
bisko
foreach ($val in $array) if ($val == "") poppinThangs(); Anything else? :p
peirix
+1  A: 

the "split" function has been deprecated, it is better not to use it.

Instead, use "explode" on <b> and then prepend back onto each element, and pop the first element off the array.

Evernoob
how do i pop the first element? i only seen poping of the last
An employee
Pop is a function that removes the first element off a 'stack' type data structure. Dequeue is a function that removes the last element out of a 'queue' type data structure. PHP arrays can be used as either depending on the functions you apply against it.
Josh Smeaton