假设我有一个包含一些 HTML 的字符串。我想在到达第一个 p
标记之前删除每个 li
标记。
我如何实现这样的目标?
示例字符串:
$str = "<img src='something.png'/>some_text_here<li>needs_to_be_removed</li>
<li>also_needs_to_be_removed</li>some_other_text<p>finally</p>more_text_here
<li>this_should_not_be_removed</li>";`
需要删除前两个 li
标记。
最佳答案
这就是您所需要的。简单有效:
$mystring = "mystringwith<li>toberemovedstring</li><li>againremove</li><p>do not remove me</p>";//the string you provide
$findme = '<li>';//the string you want to search in $mystring
$findpee = '<p>';//haha pee also where to end it
$pos = strpos($mystring, $findme);//first position of <li>
$pospee = strpos($mystring, $findpee);// then position of pee.. get it :)
//Then we remove it
$result=substr_replace ( $mystring ,"" , $pos, ($pospee-$pos));
echo $result;
编辑:PHP 沙箱
http://sandbox.onlinephpfunctions.com/code/e534259e2312682a04b64c6e3aae1521422aacd2
您也可以在此处查看结果
关于php - 在到达字符串中的第一个 p 标签之前删除每个 li 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35155542/