PHP 只允许 img 标签

发布于 2024-10-03 23:33:45 字数 455 浏览 2 评论 0原文

我需要你的帮助相关的php。在 php 中,我只想允许 html 标签,我尝试了 php 的内置函数 strip_tags() 但它没有给我需要的输出。例如,在下面的代码中 strip_tags() 允许使用 img 标签,但与文本一起使用。

$img = "<img src='/img/fawaz.jpg' alt= ''> <br /> <p> This is a detailed paragraph about Fawaz and his mates.</p>";
echo strip_tags($img , "<img>");

仅允许来自函数或变量的 或任何标记的正确方法是什么。 任何帮助将不胜感激。

谢谢

I need your assistence related php. In php, i want to allow html <img> tags only, i tried php's built-in function strip_tags() but it's not giving me the output i need. For instance, in the following code strip_tags() allows img tags but along with text.

$img = "<img src='/img/fawaz.jpg' alt= ''> <br /> <p> This is a detailed paragraph about Fawaz and his mates.</p>";
echo strip_tags($img , "<img>");

What would be the proper way to just allow <img> or any tag only from the function or variable.
Any help 'd be appreciated.

Thanks

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

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

发布评论

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

评论(3

尝蛊 2024-10-10 23:33:45

这可能是由于代码中未关闭 img 标记造成的。试试这个

$img = "<img src='/img/fawaz.jpg' alt= '' /> <br /> <p> This is a detailed paragraph about Fawaz and his mates.</p>";
echo strip_tags($img , "<img>");

This might be due to non closing img tag in your code. Try this

$img = "<img src='/img/fawaz.jpg' alt= '' /> <br /> <p> This is a detailed paragraph about Fawaz and his mates.</p>";
echo strip_tags($img , "<img>");
又怨 2024-10-10 23:33:45

strip_tags() 不能按照您希望的方式工作。如果提供第二个参数,则允许列出的标签成为结果字符串的一部分 - 未列出的标签除外。并且它不会过滤掉内部文本。

如果您只想提取 元素,甚至不要考虑使用正则表达式。为此使用 DOM 解析器:

libxml_use_internal_errors(true);
$doc=new DOMDocument;
$html=$doc->loadHTML('<img src="/img/fawaz.jpg" alt= ""> <br /> <p> This is a
detailed paragraph about Fawaz and his mates.</p>');
$path=new DOMXPath($doc);
foreach ($path->query('//img') as $found)
    var_dump($doc->saveXML($found));

strip_tags() doesn't work that way you want it to behave. If supplied with a second argument, the tags listed are allowed to be part of the resulting string - except those which are not listed. And it will not filter out inner text.

If you want to extract <img/> elements only, don't even think about using a regex. Use a DOM parser for that:

libxml_use_internal_errors(true);
$doc=new DOMDocument;
$html=$doc->loadHTML('<img src="/img/fawaz.jpg" alt= ""> <br /> <p> This is a
detailed paragraph about Fawaz and his mates.</p>');
$path=new DOMXPath($doc);
foreach ($path->query('//img') as $found)
    var_dump($doc->saveXML($found));
掀纱窥君容 2024-10-10 23:33:45

删除没有


gt 的 HTML 标签;
和...

$img = "
        <img src='/img/fawaz.jpg' alt= '' />
        <br /><br/>
        <hr/>
        <p> This is a detailed paragraph about Fawaz and his mates.</p>
        <a href='cft'>123</a>
        ";
$img = strip_tags($img , "<img>|<a>|<br>|<hr>");
echo $img;

delete HTML Tags Without <img> and <a> and <br/> and <hr/> and ...

$img = "
        <img src='/img/fawaz.jpg' alt= '' />
        <br /><br/>
        <hr/>
        <p> This is a detailed paragraph about Fawaz and his mates.</p>
        <a href='cft'>123</a>
        ";
$img = strip_tags($img , "<img>|<a>|<br>|<hr>");
echo $img;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文