开发者

How to phpexplode a string with code in it?

I am trying to use php's explode to divide a string into two. However that string is html code and php does not seem to agree.

This is what works:

<?

$pizza  = "hello<!--a-->world";
$pieces = explode("<!--a-->", $pizza);
echo $pieces[0]; 
echo "<br/><br/>";
echo $pieces[1]; 

?>

This does not work, I receive this error http://pelican-cement.com/exp3.php

<?

$pizza  = "<div id=main><div id=cnt><div id=sfcnt><div id=sform style="height:43px"></div><div class=tsf-p style=visibility:hidden></div></div><noscript><style>.bl{display:none !important}</style></noscript><div id=subform_ctrl><div style="float:right"><a href="/advanced_search?q=hotels&amp;hl=en&amp;prmd=ivnscm" class="gl nobr" style="color:#36c">Advanced search</a></div><div><div id=resultStats>Ab开发者_开发知识库out 462,000,000 results<nobr>  (0.18 seconds)&nbsp;</nobr></div></div></div><div id=nr_container style="position:relative;zoom:1"><div id=center_col><div id=res class=med role=main><div id=topstuff></div><!--a--><h2 class=hd>Search Results</h2>";
$pieces = explode("<!--a-->", $pizza);
echo $pieces[0]; 
echo "<br/><br/>";
echo $pieces[1]; 

?>

The only difference is in the strings. I am new to PHP, is there a problem with using html in strings?

Any help would be much appreciated.


In fact you must escape the " in your html code. Two methods :

$pizza  = "<div id=main><div id=cnt><div id=sfcnt><div id=sform style=\"height:43px\">...";

Or by changing to signle quotes :

$pizza  = '<div id=main><div id=cnt><div id=sfcnt><div id=sform style="height:43px">...';

Good luck.

Niels


Your html contains double quotes ("). These must be escaped to make a valid php string

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜