允许注释的php strip_标记

允许注释的php strip_标记,php,strip-tags,Php,Strip Tags,我需要剥离所有html标签,但保留注释行提取信息 有可能吗 $content = strip_tags($content, '<!-->'); $content=strip_标签($content,”); 这不起作用,我尝试了几种不同的变体。使用以下正则表达式代替strip_tags(): $szRetVal = preg_replace( '%</?[a-z][a-z0-9]*[^<>]*>%sim','',$szHTML ); $szRetVal=p

我需要剥离所有html标签,但保留注释行提取信息

有可能吗

$content = strip_tags($content, '<!-->');
$content=strip_标签($content,”);

这不起作用,我尝试了几种不同的变体。

使用以下正则表达式代替strip_tags():

$szRetVal = preg_replace( '%</?[a-z][a-z0-9]*[^<>]*>%sim','',$szHTML );
$szRetVal=preg_replace('%%sim',''.$szHTML);

不要使用strip_标记()使用以下正则表达式:

$szRetVal = preg_replace( '%</?[a-z][a-z0-9]*[^<>]*>%sim','',$szHTML );
$szRetVal=preg_replace('%%sim',''.$szHTML);

您可以使用以下代码在删除注释之前保护注释

// create a random string for using in replace strings
$random = strtoupper(dechex(rand(0,10000000000)));
// replace comment starts
$html = preg_replace('/<!--/', '@MARKER-START-'. $random.'@', $html);
// replace comment ends
$html = preg_replace('/-->/', '@MARKER-END-'. $random.'@', $html);
// strip all html tags
$html = strip_tags($html);
// replace back comment starts
$html = preg_replace('/@MARKER-START-'. $random.'@/', '<!--', $html);
// replace back comment ends
$html = preg_replace('/@MARKER-END-'. $random.'@/', '-->', $html);
//创建用于替换字符串的随机字符串
$random=strtoupper(十进制(兰特(010000000000));
//替换注释开始
$html=preg_replace(“//”、“@MARKER-END-”.$random.@、$html);
//去除所有html标记
$html=带标签($html);
//替换回注释开始
$html=preg_replace('/@MARKER-START-'.$random'.@/',''.$html);

您可以使用以下代码在删除注释之前保护注释

// create a random string for using in replace strings
$random = strtoupper(dechex(rand(0,10000000000)));
// replace comment starts
$html = preg_replace('/<!--/', '@MARKER-START-'. $random.'@', $html);
// replace comment ends
$html = preg_replace('/-->/', '@MARKER-END-'. $random.'@', $html);
// strip all html tags
$html = strip_tags($html);
// replace back comment starts
$html = preg_replace('/@MARKER-START-'. $random.'@/', '<!--', $html);
// replace back comment ends
$html = preg_replace('/@MARKER-END-'. $random.'@/', '-->', $html);
//创建用于替换字符串的随机字符串
$random=strtoupper(十进制(兰特(010000000000));
//替换注释开始
$html=preg_replace(“//”、“@MARKER-END-”.$random.@、$html);
//去除所有html标记
$html=带标签($html);
//替换回注释开始
$html=preg_replace('/@MARKER-START-'.$random'.@/',''.$html);

Its在手册中:
注意:HTML注释和PHP标记也被剥离。这是硬编码的,不能使用允许的标记进行更改。
,您可能需要为此创建一个解决方案,一个HTML解析器perhapsthanks。我将使用preg replace删除标记并删除注释。@PotentialCoder只需将注释替换为其他内容(例如,实体),然后在
strip_tags
后将其返回。其在手册中:
注意:HTML注释和PHP标记也被剥离。这是硬编码的,不能使用允许的标记进行更改。
,您可能需要为此创建一个解决方案,一个HTML解析器perhapsthanks。我将使用preg replace删除标记并删除注释。@PotentialCoder只需将注释替换为其他内容(例如实体),并在
删除标记后返回注释。
。哎呀。。。正则表达式发布后出错:“%sim'Oops…”。。。正则表达式发布后出错:“%sim”为什么需要添加随机字符串?是否在注释中有实际字符串“@MARKER-START-@”的情况下?是的。如果注释中有实际字符串“@MARKER-START-@”,那么为什么需要添加随机字符串?是否在注释中有实际字符串“@MARKER-START-@”的情况下?是的。如果注释中有实际字符串'@MARKER-START-@',则正好是这种情况