Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_更换的问题_Php - Fatal编程技术网

Php preg_更换的问题

Php preg_更换的问题,php,Php,这是一个简单的例子: 我有一句话很管用: $listing['biz_description'] = preg_replace('/<!--.*?--\>/','',$listing['biz_description']); 如果你对preg_replace regex感到满意的话,我会解码html实体。。。正如@ircmaxell所提到的,使用正则表达式进行html解析可能非常痛苦 $str = "This is a <!-- test --> of the emer

这是一个简单的例子:

我有一句话很管用:

$listing['biz_description'] = preg_replace('/<!--.*?--\>/','',$listing['biz_description']);

如果你对preg_replace regex感到满意的话,我会解码html实体。。。正如@ircmaxell所提到的,使用正则表达式进行html解析可能非常痛苦

$str = "This is a <!-- test --> of the emergency &lt;!-- broadcast --&gt; system";
$str = preg_replace('/<!--.*?--\>', '' ,html_entity_decode($str));
echo $str;

DomDocument采用有效xml的实现:

$dom = new DomDocument();
$dom->loadXml($listing['biz_description']);
removeComments($dom);
$listing['biz_description'] = $dom->saveXml();

function removeComments(DomNode $node) {
    if ($node instanceof DomComment) {
        $node->parentNode->removeChild($node);
    } else {
        foreach ($node->childNodes as $child) {
            removeComments($child);
        }
    }
}

duh,我早就应该想到这个lol了。正则表达式所做的就是解析一个描述字段,这样对服务器来说就不太麻烦了。谢谢
$dom = new DomDocument();
$dom->loadXml($listing['biz_description']);
removeComments($dom);
$listing['biz_description'] = $dom->saveXml();

function removeComments(DomNode $node) {
    if ($node instanceof DomComment) {
        $node->parentNode->removeChild($node);
    } else {
        foreach ($node->childNodes as $child) {
            removeComments($child);
        }
    }
}