简体   繁体   English

PHP:字符转义不能与双引号一起使用,除非有 <pre> 标签?

[英]PHP: Character escaping not working with double quotes unless there is a <pre> tag?

Please can someone tell me where I'm going wrong? 请有人能告诉我我要去哪里错了吗?

//outputs : Hi, my name is Jackal I like PHP
<?php
echo "Hi, my name is Jackal \nI like PHP";
?>

Whereas if I use pre tag 而如果我使用pre标签

//outputs: Hi, my name is Jackal
//         I like PHP
<pre>
<?php
echo "Hi, my name is Jackal \nI like PHP";
?>

Can someone please explain why character escaping isn't working? 有人可以解释为什么转义字符不起作用吗? thanks 谢谢

PHP is interpreting that line break. PHP 正在解释该换行符。 Look in the source code of the webpage. 查看网页的源代码。 You'll see that you have 2 lines in the source. 您会看到源中有2行。

However, HTML handles line breaks differently. 但是,HTML处理换行符的方式有所不同。 To perform a line break in HTML, use the <br> tag. 要在HTML中执行换行符,请使用<br>标记。 This will make the HTML output over 2 lines on the webpage itself. 这将使HTML在网页本身上的两行输出。 However, in the source code, it will still appear as a single line (unless you add that line break). 但是,在源代码中,它仍将显示为一行(除非您添加该换行符)。

What <pre> is doing is telling the HTML engine to output the exact string as-is since the text is preformatted, including interpreting that line break. <pre>作用是告诉HTML引擎按原样输出确切的字符串,因为文本是经过预先格式化的,包括解释该换行符。 Also note, you should add a closing </pre> tag at the end of the block that is preformatted. 另请注意,您应该在预格式化的代码块末尾添加一个</pre>标记。

If you have this in your HTML: 如果您的HTML中包含以下内容:

<p>Hello,
World!</p>

Does it appear on one line, or two? 它出现在一行还是两行上?

Usually, the answer is one: whitespace is condensed into a single space. 通常,答案是一个:空格被压缩为一个空格。

<pre> , however, has the default CSS of white-space:pre , which does NOT condense whitespace. 但是, <pre>具有默认的CSS,即white-space:pre ,它不会压缩空格。

You should echo "<br />" , or apply white-space:pre (or, even better, white-space:pre-wrap ) to the container element. 您应该echo "<br />" ,或者将white-space:pre (或者更好的是, white-space:pre-wrap )应用于容器元素。

Because in your browser \\n shows just a whitespace and does not appear as line break unless you use pre-formatted HTML tag <pre> . 因为在您的浏览器中, \\n仅显示空格,并且不会显示为换行符,除非您使用预格式化的HTML标签<pre>

eg following will display with a line break in browser: 例如,以下将在浏览器中显示换行符:

echo "Hi, my name is Jackal <br />I like PHP";

or this one also: 或者这也是:

echo "<pre>Hi, my name is Jackal \nI like PHP</pre>";

As you can make out that <pre> tag is for displaying pre-formatted text hence \\n does show as line break. 如您所见, <pre>标记用于显示预格式化的文本,因此\\n确实显示为换行符。

Character escaping is working. 字符转义正在工作。 Look at the source code, there you'll find the new line. 查看源代码,您将在其中找到新行。 In order to make it visible in a browser you need an html tag ( <br /> ) or the wrapping <pre> 为了使其在浏览器中可见,您需要一个html标签( <br /> )或包装的<pre>

<br> 

is HTML. 是HTML。

<br />

is XHTML (recommended). 是XHTML(推荐)。

\n

is a newline in the code (works while writing to file, but doesn't output to the screen). 是代码中的换行符(可在写入文件时使用,但不会输出到屏幕)。

alternately you can use nl2br as below 或者,您可以按以下方式使用nl2br

echo nl2br("Hi, my name is Jackal \nI like PHP");

The newline character in php actually echo sa new line itself. PHP中的newline实际上echo newline本身。

However, the output of php's echo statement is still the source code of the web browser. 但是,php的echo语句的输出仍然是Web浏览器的源代码。 Hence, the browser condenses all the extra line breaks and spaces. 因此,浏览器会压缩所有多余的换行符和空格。 For example: 例如:

<p>Hello
    there</p>

outputs 输出

Hello there

despite the white space. 尽管有空白。 To get around this issue, use the <br> / <br/> tag to tell the browser to make a newline, instead of tell php to make a newline. 要解决此问题,请使用<br> / <br/>标记告诉浏览器进行换行,而不是告诉php进行换行。

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

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