使用 php 剥离 html 标签

发布于 2024-10-06 22:46:57 字数 295 浏览 5 评论 0原文

内的内容之外的html标记

我怎样才能去除除预标记代码

$content="
    <div id="wrapper">

    Notes


    </div>


    <pre>
    <div id="loginfos">asdasd</div>

    </pre>
";

当使用strip_tags($content,'')时,预标记内的html也被剥离了。但我不想把里面的 html 预剥离掉

How can i strip html tag except the content inside the pre tag

code

$content="
    <div id="wrapper">

    Notes


    </div>


    <pre>
    <div id="loginfos">asdasd</div>

    </pre>
";

While using strip_tags($content,'') the html inside the pre tag too stripped of. but i don't want the html inside pre stripped off

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

双马尾 2024-10-13 22:46:57

尝试 :

echo strip_tags($text, '<pre>');

Try :

echo strip_tags($text, '<pre>');
旧伤还要旧人安 2024-10-13 22:46:57

您可以执行以下操作:

  1. 使用 preg_replace 和 'e' 修饰符将 pre 标记的内容替换为一些字符串,如 @@@1@@@、@@@2@@@ 等,同时将此内容存储在某个数组中
  2. 运行 strip_tags ()
  3. 再次运行带有'e'修饰符的preg_relace,将@@@1@@@等恢复为原始内容。

有点笨拙但应该可以。

You may do the following:

  1. Use preg_replace with 'e' modifier to replace contents of pre tags with some strings like @@@1@@@, @@@2@@@, etc. while storing this contents in some array
  2. Run strip_tags()
  3. Run preg_relace with 'e' modifier again to restore @@@1@@@, etc. into original contents.

A bit kludgy but should work.

小伙你站住 2024-10-13 22:46:57
 <?php 
                    $document=html_entity_decode($content);
                    $search = array ("'<script[^>]*?>.*?</script>'si","'<[/!]*?[^<>]*?>'si","'([rn])[s]+'","'&(quot|#34);'i","'&(amp|#38);'i","'&(lt|#60);'i","'&(gt|#62);'i","'&(nbsp|#160);'i","'&(iexcl|#161);'i","'&(cent|#162);'i","'&(pound|#163);'i","'&(copy|#169);'i","'&#(d+);'e");
                    $replace = array ("","","\1","\"","&","<",">"," ",chr(161),chr(162),chr(163),chr(169),"chr(\1)");
                    $text = preg_replace($search, $replace, $document); 
                    echo $text;
                    ?>
 <?php 
                    $document=html_entity_decode($content);
                    $search = array ("'<script[^>]*?>.*?</script>'si","'<[/!]*?[^<>]*?>'si","'([rn])[s]+'","'&(quot|#34);'i","'&(amp|#38);'i","'&(lt|#60);'i","'&(gt|#62);'i","'&(nbsp|#160);'i","'&(iexcl|#161);'i","'&(cent|#162);'i","'&(pound|#163);'i","'&(copy|#169);'i","'&#(d+);'e");
                    $replace = array ("","","\1","\"","&","<",">"," ",chr(161),chr(162),chr(163),chr(169),"chr(\1)");
                    $text = preg_replace($search, $replace, $document); 
                    echo $text;
                    ?>
感性 2024-10-13 22:46:57
$text = 'YOUR CODE HERE';

$org_text = $text;

// hide content within pre tags
$text = preg_replace( '/(<pre[^>]*>)(.*?)(<\/pre>)/is', '$1@@@pre@@@$3', $text );
// filter content
$text = strip_tags( $text, '<pre>' );
// insert back content of pre tags
if ( preg_match_all( '/(<pre[^>]*>)(.*?)(<\/pre>)/is', $org_text, $parts ) ) {
    foreach ( $parts[2] as $code ) {
        $text = preg_replace( '/@@@pre@@@/', $code, $text, 1 );
    }
}

print_r( $text );
$text = 'YOUR CODE HERE';

$org_text = $text;

// hide content within pre tags
$text = preg_replace( '/(<pre[^>]*>)(.*?)(<\/pre>)/is', '$1@@@pre@@@$3', $text );
// filter content
$text = strip_tags( $text, '<pre>' );
// insert back content of pre tags
if ( preg_match_all( '/(<pre[^>]*>)(.*?)(<\/pre>)/is', $org_text, $parts ) ) {
    foreach ( $parts[2] as $code ) {
        $text = preg_replace( '/@@@pre@@@/', $code, $text, 1 );
    }
}

print_r( $text );
離人涙 2024-10-13 22:46:57

好吧!,你只留下一个选择:正则表达式......没有人喜欢它们,但它们确实能完成工作。首先,用一些奇怪的东西替换有问题的文本,如下所示:

preg_replace("#<pre>(.+?)</pre>#", "||k||", $content);

这将有效地将您的内容更改

<pre> blah, blah, bllah....</pre>

为其他内容,然后调用

strip_tags($content);

之后,您可以替换 ||k|| 中的原始值(或您选择的任何内容),然后您'就会得到想要的结果。

Ok!, you leave nothing but one choice: Regular Expressions... Nobody likes 'em, but they sure get the job done. First, replace the problematic text with something weird, like this:

preg_replace("#<pre>(.+?)</pre>#", "||k||", $content);

This will effectively change your

<pre> blah, blah, bllah....</pre>

for something else, and then call

strip_tags($content);

After that, you can just replace the original value in ||k||(or whatever you choose) and you'll get the desired result.

忱杏 2024-10-13 22:46:57

我认为您的内容在 $content 变量中存储得不是很好,

您可以通过将内部双引号转换为单引号来检查一次吗

$content="
    <div id='wrapper'>

    Notes


    </div>


    <pre>
    <div id='loginfos'>asdasd</div>

    </pre>
";


strip_tags($content, '<pre>');

I think your content is not stored very well in the $content variable

could you check once by converting inner double quotes to single quotes

$content="
    <div id='wrapper'>

    Notes


    </div>


    <pre>
    <div id='loginfos'>asdasd</div>

    </pre>
";


strip_tags($content, '<pre>');
ら栖息 2024-10-13 22:46:57

您可以执行以下操作:

使用带有 'e' 修饰符的 preg_replace 将 pre 标记的内容替换为一些字符串,例如 @@@1@@@@@ @2@@@ 等,同时将此内容存储在某个数组中
运行strip_tags()

再次运行带有'e'修饰符的preg_relace,将@@@1@@@等恢复为原始内容。

有点笨拙但应该可以。

您能写出完整的代码吗?我明白了,但出了点问题。请写出完整的编程代码

You may do the following:

Use preg_replace with 'e' modifier to replace contents of pre tags with some strings like @@@1@@@, @@@2@@@, etc. while storing this contents in some array
Run strip_tags()

Run preg_relace with 'e' modifier again to restore @@@1@@@, etc. into original contents.

A bit kludgy but should work.

Could you please write full code. I understood, but something goes wrong. Please write full programming code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文