PHP - 使用strip_tags删除已删除标记之间的文本

时间:2016-09-29 17:21:34

标签: php strip-tags

我有这个示例代码:

<?php

$string='Left text from tag <div title="hello world" class="CSS">What is <b>going on</b> here?<br> Calm up <em>right now</em>.</div> Right text. Possible another <div title="" class="DD">tag..</div> but not always.';

echo strip_tags($string);
?>

此代码的结果是:

Left text from tag What is going on here? Calm up right now. Right text. Possible another tag.. but not always.

但是,我的目标是删除此strip_tags函数删除的标记之间的所有文本(包括标记)。 IE浏览器。结果应该是:

Left text from tag Right text. Possible another but not always.

我知道可以使用preg_replace来完成它,但它太慢了,所以可能有更快的解决方案..(不一定与strip_tags函数有关)。

2 个答案:

答案 0 :(得分:1)

在我看来,使用REGEX是最好和最紧凑的解决方案。试试这个:

echo preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $string);

如果您不想使用preg_replace,请使用手册中提到的自定义功能strip_tags_content()

function strip_tags_content($text, $tags = '', $invert = FALSE) { 

    preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags); 
    $tags = array_unique($tags[1]); 

    if(is_array($tags) AND count($tags) > 0) { 
        if($invert == FALSE) { 
            return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text); 
         } else { 
             return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text); 
         } 
     } elseif($invert == FALSE) { 
           return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text); 
     } 
     return $text; 
 } 

echo strip_tags_content($string);

注意:我不认为只能使用PHP函数实现所需的输出。您需要以某种方式使用REGEX。

答案 1 :(得分:1)

DOMDocument方法怎么样?

<?php

$string='Left text from tag <div title="hello world" class="CSS">What is <b>going on</b> here?<br> Calm up <em>right now</em>.</div> Right text. Possible another <div title="" class="DD">tag..</div> but not always.';

$dom = new DomDocument();
$dom->loadHTML('<body>' . $string . '</body>');

$stripped = '';
$els = $dom->getElementsByTagName('body')->item(0)->childNodes;
$len = count($els) - 1;
foreach($els as $index => $child) {
    if (is_null($child->tagName))
        $stripped .= ' ' . trim($child->nodeValue);
}
$stripped = substr($stripped, 1);

echo $stripped;

Output

Left text from tag Right text. Possible another but not always.
相关问题