PHP MySql显示返回的内容而不剥离html标签

发布于 2024-10-20 17:11:23 字数 493 浏览 3 评论 0原文

我有一个 SQL“文本”数据类型列,里面有 html 标记内的内容,例如

一些内容:

  • 两个
... I want the mysql query to echo out the databases contents without stripping the html tags (its possible this is done by php for security reasons or?) so that the html code will render if you get me? At the moment it just lumps out a paragraph which looks aweful! It should be noted security is not much of a concern as this is a project and not going to be exposed publicly

干杯伙计

尼克

I have a column in SQL 'Text' datatype and inside I have content within html tag, eg

somecontent:

  • one
  • two

... I want the mysql query to echo out the databases contents without stripping the html tags (its possible this is done by php for security reasons or?) so that the html code will render if you get me? At the moment it just lumps out a paragraph which looks aweful! It should be noted security is not much of a concern as this is a project and not going to be exposed publicly

Cheers folks

Nick

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

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

发布评论

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

评论(5

披肩女神 2024-10-27 17:11:23

MySQL 不会从文本中去除标签——它不在乎文本是什么。 PHP 也不会删除标签,除非您在代码中的某个位置执行了 strip_tags() 或等效操作。

如果你想强制浏览器显示检索到的数据中的标签,可以通过 [htmlspecialchars()][1] 运行字符串,它会转换 html 元字符 (<、>"& 等...)转换为它们的字符实体等效项 (<> 等...)。

或者您可以通过执行以下操作强制将整个页面呈现为纯文本。

header('Content-type: text/plain');

MySQL wouldn't strip tags from text - it couldn't care less what the text is. PHP also wouldn't strip tags, unless somewhere in your code you do a strip_tags() or equivalent.

If you want to force the browser to display the tags in the retrieved data, you can run the string through [htmlspecialchars()][1], which converts html metacharacters (<, >, ", &, etc...) to their character entity equivalents (<, >, etc...).

Or you can force the entire page to be rendered as plain text by doing

header('Content-type: text/plain');
指尖微凉心微凉 2024-10-27 17:11:23

除非您明确告知,否则 PHP 不会删除数据库内容。
您确定您的标签在插入之前没有被剥离吗?

或者尝试 stripslashes();

Database content isn't stripped by PHP unless you explicitly tell it to.
Are you sure your tags haven't been stripped before they were inserted?

Alternatively try stripslashes();

爱,才寂寞 2024-10-27 17:11:23

使用 htmlspecialchars_decode() 函数。它对我来说效果很好...

使用指南

您的文本与代码

<?php

$text = " 
<html>
<head>
    <title>Page Title</title>
</head>
<body>
     Reference site about Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.
</body>
</html> ";

OR

$text = $row['columnname'];  // Here define your database table column name.

echo htmlspecialchars_decode($text);  

?>

输出:

关于 Lorem Ipsum 的参考站点,提供有关其起源的信息,以及随机 Lipsum 生成器。

Use htmlspecialchars_decode() function. Its works fine for me...

Use guideline

Your Text with code

<?php

$text = " 
<html>
<head>
    <title>Page Title</title>
</head>
<body>
     Reference site about Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.
</body>
</html> ";

OR

$text = $row['columnname'];  // Here define your database table column name.

echo htmlspecialchars_decode($text);  

?>

Output:

Reference site about Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.

时间海 2024-10-27 17:11:23

这对我有用:

例如:

<?=htmlspecialchars_decode(htmlspecialchars('I <b>love</b> you'))?>

您的浏览器将输出:

this works for me:

Ex:

<?=htmlspecialchars_decode(htmlspecialchars('I <b>love</b> you'))?>

Your browser will output:
I love you

内心旳酸楚 2024-10-27 17:11:23

使用以下代码片段:

echo(strip_tags($your_string));

复制。

Use bellow snippet:

echo(strip_tags($your_string));

copied.

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