我发现这个概念与我的需求相同: -
Remove character starting with x and ending with x and everything between it in a string
在PHP中会有类似的功能吗?还有,可以用长字符串替换所有这些情况吗?
例如:
$ text =“
<h2 class="active" tabindex="0" rel="120">Title 2</h2><h2 class="active" tabindex="0" list="20" rel="120">Title 2</h2><h2 class="active" tabindex="0" rel="120">Title 2</h2>
“;
在这里,我想检测class =“active”和“&gt;” </h2>
,删除任何更改内容。
答案 0 :(得分:0)
PHP中的等价物是这样的:
function remove_between($str, $anchor)
{
$anchor = preg_quote($anchor, '/');
return preg_replace("/$anchor(.*)$anchor/U", '', $str);
}
remove_between('123aa456aa789', 'aa'); // 123789