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 <!-- broadcast --> 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);
}
}
}