字符串在起始字符串和结束字符串之间删

时间:2013-05-24 08:47:48

标签: php

我发现这个概念与我的需求相同: -

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>,删除任何更改内容。

1 个答案:

答案 0 :(得分:0)

PHP中的等价物是这样的:

function remove_between($str, $anchor)
{
    $anchor = preg_quote($anchor, '/');

    return preg_replace("/$anchor(.*)$anchor/U", '', $str);
}

remove_between('123aa456aa789', 'aa'); // 123789