我有一个类似下面的输出:
<img src="location-of-image.png" style="width:100px;height:100px" title='smpl'/>
<p>sampe sample sampple</p><br />when? how?.我这里的问题是我只想
<img src="location-of-image.png" style="width:100px;height:100px;" title='smpl'/>并删除
<p>sampe sample sampple</p><br />when? how?.`所以我的预期输出应该是这样的:
<img src="location-of-image.png" style="width:100px;height:100px;" title='smpl'/>我如何在PHP中做到这一点?我知道这在javascript/jquery中更容易完成,但我希望它在PHP中。
发布于 2012-03-26 20:01:15
使用PHP DOM:
$doc = new DOMDocument();
$doc->loadXML('<img src="location-of-image.png" style="width:100px;height:100px;"    title='sampletitle' /><p>sampe sample sampple</p>');
$doc = $doc->removeChild($doc->firstChild);发布于 2012-03-26 19:53:49
您可以使用str_replace
$str  =  '<img src="location-of-image.png" style="width:100px;height:100px;" title='sampletitle' /><p>sampe sample sampple</p><br />when? how?.';
$toBeReplaced   =  '<p>sampe sample sampple</p><br />when? how?.';
$body = str_replace($toBeReplaced, "", $str);发布于 2012-03-26 19:59:24
您可以使用preg_replace
preg_replace('^(<img.*?/>).*$', 
             '$1',
             '<img src="location-of-image.png" style="width:100px;height:100px;" title="sampletitle" /><p>sampe sample sampple</p><br />when? how?.');https://stackoverflow.com/questions/9871542
复制相似问题