我有一个类似下面的输出:
<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:51:39
好吧,我想我想通了。
这是我的答案,一个非常明显和简单的解决方案:
$str = '<img src="location-of-image.png" style="width:100px;height:100px" title='smpl'/ <p>sampe sample sampple</p><br />when? how?.'
$str = strip_tags( $str, "<img>"); #For safety precaution.
$first = strpos($str, '<img ');
$last = strpos($str, '/>');
echo "<".substr( $str ,1,$last+1 );感觉我得到了正确的答案。
感谢所有回复的人!:)
https://stackoverflow.com/questions/9871542
复制相似问题