explode glitch in delimiter
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. Tha开发者_如何学Pythont 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!
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
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 < and >, which it cannot find the in the string, so it just returns the whole string.
精彩评论