繁体   English   中英

剥离标签(<和>)

[英]Strip tags (< and >)

我正在寻找剥离的方法< > 以及PHP之间的所有内容。 并将其保存为变量。

例:

由此得出: <p>This is a paragraph with <strong>bold</strong> text</p> <p>This is a paragraph with <strong>bold</strong> text</p>

对此: This is a paragraph with bold text

有人有例子或想法吗? 谢谢!

如果你没有嵌套> < ,那么你可以尝试以下来匹配出现:

$matches = array();
preg_match_all('/<([\s\S]*?)>/s', $string, $matches);

在这里试试你的。 注意? 在查询中,使括号中的匹配不合适。 您可以在此处找到类似问题的答案。

如果要删除值,请使用preg_replace_callback

<?php
$string = '&lt;p&gt;This is a paragraph with &lt;strong&gt;bold&lt;/strong&gt; text&lt;p&gt;';
echo "$string <br />";
$string = preg_replace_callback(
        '/&lt;([\s\S]*?)&gt;/s',
        function ($matches) {
            // do whatever you need with $matches here, e.g. save it somewhere
            return '';
        },
        $string
    );
echo $string;
?>

<encoding是html编码。 要处理这个问题,你需要html_entity_decode()来解码或者htmlentities()来编码。

像这样的东西:

$matches = array();
// Save
preg_match_all('!(<[^>]++>)!', $string, $matches);
// Strip
$string = strip_tags($string);

或者替换< with &lt; >&gt; 如果这不是一个拼写错误,你想对已经转义的字符串进行操作,并使用preg_replace('/(&lt;.+?&gt;)/', '', $string)来删除标签。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM