在 PHP 中回显新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7686818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:10:55  来源:igfitidea点击:

Echo new line in PHP

php

提问by test123123

<?php
    include 'db_connect.php';
    $q = mysql_real_escape_string($_GET['q']);
    $arr = explode('+', $q);

    foreach($arr as $ing)
    {
        echo $ing;
        echo "<br/>";
    }
    mysql_close($db);
?>

Calling:

调用:

findByIncredients.php?q=Hans+Wurst+Wurstel

Source code HTML:

源代码 HTML:

Hans Wurst Wurstel<br/>

Why is there only one newline?

为什么只有一个换行符?

回答by Jan Zyka

+s in URL are urlencoded spaces. So what php sees in the variable is "Hans Wurst Wurstel". You need to split by space ' ', not +

URL 中的 +s 是 urlencoded 空格。所以php在变量中看到的是“Hans Wurst Wurstel”。您需要按空格 ' ' 分割,而不是 +

arr = explode (' ',$q);

回答by DhruvPathak

"+" gets converted to SPACE on URL decoding. You may want to pass your string as str1-str2-str3 in get parameter.

“+”在 URL 解码时被转换为空格。您可能希望在 get 参数中将字符串作为 str1-str2-str3 传递。

回答by SW4

Try:

尝试:

<?php
include 'db_connect.php';
$q = mysql_real_escape_string($_GET['q']);
$arr = explode (' ',$q);

foreach($arr as $ing)
{
echo $ing;
echo "<br/>";
}

mysql_close($db);

?>

回答by etuardu

Hans+Wurst+Wurstelis the url escaped query string. The php page will likely process it once unescaped (in this case, all +s will be translated into spaces). You should choose a delimiter for explodeaccording to the string as it is in that moment. You can use print_r()for a raw print if you don't know how the string (or any kind of variable) looks like.

Hans+Wurst+Wurstel是 url 转义查询字符串。一旦未转义,php 页面可能会处理它(在这种情况下,所有+s 都将被转换为空格)。您应该explode根据当时的字符串选择一个分隔符。print_r()如果您不知道字符串(或任何类型的变量)的外观,您可以将其用于原始打印。

回答by igorw

Easy. While the standard RFC 3986 url encoding would encode the space " " as "%20", due to historical reasons, it can also be encoded as "+". When PHP parses the query string, it will convert the "+" character to a space.

简单。虽然标准 RFC 3986 url 编码会将空格“”编码为“%20”,但由于历史原因,它也可以编码为“+”。PHP 解析查询字符串时,会将“+”字符转换为空格。

This is also illustrated by the existence of both:

两者的存在也说明了这一点:

  • urlencode: equivalent of what PHP uses internally, will convert " " to "+".
  • rawurlencode: RFC-conformant encoder, will convert " " to "%20".
  • urlencode:相当于 PHP 内部使用的,将“”转换为“+”。
  • rawurlencode: 符合 RFC 的编码器,将“”转换为“%20”。

I'm assuming you want to explodeby space. If you really wanted to encode a "+" character, you could use "%2B", which is the rawurlencodeversion and will always work.

我假设你想explode通过空间。如果您真的想对“+”字符进行编码,则可以使用“%2B”,这是rawurlencode版本并且将始终有效。

(EDIT)

(编辑)

Related questions:

相关问题: