输出html标签

发布于 2024-12-22 20:15:18 字数 391 浏览 2 评论 0原文

我想用 echo 逐字输出 html 标签。 首先,代码是这样的:

$image = '[img]'."image_url".'[/img]<BR>';

但这不起作用,因为输出是:

*[img]image_url[/img]*

,然后换行。

然后我找到了解决方案:

$image = '[img]'."image_url".'[/img]&ltBR&gt';

我的问题是: 为什么单引号实际上并不像我想象的那样输出字符串,还有其他方法吗,这样我就不必编写所有这些 &lt&gt

I want to literally output, with echo, html tags.
First the code was like this:

$image = '[img]'."image_url".'[/img]<BR>';

but that was not working because the output was:

*[img]image_url[/img]*

and then break into new line.

Then I found a solution:

$image = '[img]'."image_url".'[/img]<BR>';

My question is:
Why single quotes don't literally output string as I thought they would and is there any other way so I don't have to write all those < and >?

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

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

发布评论

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

评论(4

ま柒月 2024-12-29 20:15:18

您的问题源于对 PHP 和 HTML 工作原理的根本误解。引号是 PHP 语法的一部分,不会影响浏览器解释带引号的字符串的方式,因为浏览器永远不会看到带引号的字符串。当 PHP 代码执行时,引号会消失,只有 PHP 脚本的输出会发送到浏览器。

也就是说,无论如何,引号并不是为了控制字符串中 HTML 标记的解释。引用样式与转义序列有关,例如 \n\t;单引号不支持转义序列(\' 除外),而双引号将它们解释为文字新行/制表符/等。这两种引用风格都与尖括号和 HTML 标签无关。

如果您希望浏览器显示大于和小于符号,则必须分别向浏览器发送编码的 HTML 实体 ><。您可以通过手动输出字符串(如 echo ">";)来实现此目的,或者更常见的是,使用专门用于对字符串中找到的 HTML 实体进行编码的 PHP 函数之一:

echo htmlspecialchars("x <= y <= z"); # outputs x <= y <= z
echo htmlspecialchars("<br>"); # outputs <br>

Your problem comes from a fundamental misunderstanding of how PHP and HTML work. The quotes, which are a part of PHP's syntax, cannot influence how the browser interprets the quoted string because the browser never sees the quoted string. The quotes fall away as the PHP code executes and only the output of your PHP script is sent to the browser.

That said, the quotes aren't intended to control the interpreting of HTML tags within strings anyways. Quote style is about escape sequences like \n and \t; single quotes do not support escape sequences (except for \') while double quotes interpret them as literal new lines/tabs/etc. Neither style of quote has anything to do with angle-brackets and HTML tags.

If you want the browser to display literal greater than and less than signs, you have to send the browser the encoded HTML entities > and < respectively. You can do this by manually outputting the strings like echo ">";, or more commonly, by using one of PHP's functions specifically intended to encode HTML entities found in strings:

echo htmlspecialchars("x <= y <= z"); # outputs x <= y <= z
echo htmlspecialchars("<br>"); # outputs <br>
丑疤怪 2024-12-29 20:15:18

它不依赖于报价。它将按原样打印(html 标签),直到您将其转换为 html 实体。请参阅htmlentities

It doesn't depend on the quotes. It will print it as it is (html tags) until you convert it into html entities. See htmlentities.

野味少女 2024-12-29 20:15:18

HTML 使用 <>要标记标签而不是 [],并且必须将 URL 作为属性 src 传递。

$image = '<img src="'."image_url".'" /><BR>';

HTML uses <> to mark tags not [] and you must pass the URL as the attribute src.

$image = '<img src="'."image_url".'" /><BR>';
淡写薰衣草的香 2024-12-29 20:15:18

您可以执行 htmlspecialchars('
')

You can do htmlspecialchars('<BR>').

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