tags:

views:

92

answers:

2

I'm having some trouble with delimiter for explode. I have a rather chunky string as a delimiter, and it seems it breaks down when I add another letter (start of a word), but it doesn't get fixed when I remove first letter, which would indicate it isn't about lenght.

To wit, the (working) code is:

$boom = htmlspecialchars("<td width=25 align=\"center\" "); 
$arr[1] = explode($boom, $arr[1]);

The full string I'd like to use is <td width=25 align=\"center\" class=\", and when I start adding in class, explode breaks down, and nothing gets done. That happens as soon as I add c, and it doesn't go away if I remove <, which it would if it's just a matter of string lenght.

Basically, the problem isn't dire, since I can just replace class=" with "" after the explode, and get the same result, but this has given me headaches to diagnose, and it seems like a really wierd problem. For what it's worth, I'm using PHP 5.3.0 in XAMPP 1.7.2.

Thanks in advance!

A: 

Have you tried adding htmlspecialchars to the explode.

$arr[1] = explode($boom, htmlspecialchars($arr[1]));

I get unexpected results without it, but with it it works perfectly.

$s = '<td width=25 align="center" class="asdjasd">sdadasd</td><td width=25 align="center" >asdasD</td>';
$boom = htmlspecialchars("<td width=25 align=\"center\" class=");
$sex = explode($boom, $s);
print_r($sex);

Outputs:

Array
(
    [0] => <td width=25 align="center" class="asdjasd">sdadasd</td><td width=25 align="center" >asdasD</td>
)

Whereas

$s = '<td width=25 align="center" class="asdjasd">sdadasd</td><td width=25 align="center" >asdasD</td>';
$boom = htmlspecialchars("<td width=25 align=\"center\" class=");

$sex = explode($boom, htmlspecialchars($s));
print_r($sex);

Outputs

Array
(
    [0] => 
    [1] => "asdjasd">sdadasd</td><td width=25 align="center" >asdasD</td>

)

This is because $boom is htmlspecialchar encoded, < and > get transformed into &lt; and &gt;, which it cannot find the in the string, so it just returns the whole string.

Psytronic
Yes, the whole string is htmlspecialchars converted. I did it in step one, when I retrieved the data. At first it was convenient to go directly to the page source, instead of displayed page, but then I kept it. As a matter of fact, the string (minus class) works perfectly, which would imply html has nothing to do with it (especially since the added char "c" is not html special character).
Xipe_Totec
A: 

You could try converting every occurrence of the delimiter in the original string

"<td width=25 align=\"center\" "

in something more manageable like:

"banana"

and then explode on that word

Anonymous
Psytronic
you are right, he probably should also check to see if the replaced delimiter is present in the original string, but it's easy to do that, and in case, to make a slight change to the substitution separator.
Anonymous
Yeah, I'm pretty sure that noone mentioned a banana on a shoe-catalogue site. ;)That's a good suggestion, and I can do it (plus few more deliminators) in one line.
Xipe_Totec
... Unless they sell shoes in a specific shade of yellow. ;)
Duroth