Return PHP QR 码扫描器中的新行

Return new line in QR code scanner in PHP

所以搜索了这里的问题,还是没有找到用户扫码换行的解决方法

我已经尝试了\n,PHP_EOL但还是没能解决。

<?php
$host = 'localhost';
$user = 'root';
$pass = 'db_pass';
$name = 'db_name';

$i = 0;
$q = $r = $s ="";
$data = $data2 = array();
$con = new mysqli($host,$user,$pass,$name);
    if($con->connect_error)
        die ("Error: ".$con->connect_error);
    $pid = ($_POST['pid']);
        $result = mysqli_query($con,"SELECT * FROM purchase_detail WHERE purchase_id ='$pid' ");

 $b=mysqli_num_rows($result);
    while($row = mysqli_fetch_array($result))
    {
        
        $data[$i] = $row['type_id'];
        $data2[$i] = $row ['quantity'];
        $i++;
    }
    
        
    
    for ($x = 0; $x < $b; $x++) 
    {   
    $q .= $data[$x];
    $r .= $data2[$x];
    $s .='Type: '.$q[$x] .' Quantity: '.$r[$x];
    echo ''.PHP_EOL.'';
    } 
 
echo "<img src='qr_img.php?d=$s'";


?>

<html>
    <form action="<?php echo htmlspecialchars ($_SERVER['PHP_SELF']);?>" method="post">
    <input type="text" name="pid">
    </form>
</html>

当前的结果是:

Type: 1 Quantity: 1Type: 2 Quantity: 1Type: 3 Quantity: 1Type: 4 Quantity: 1

但是,我希望它看起来像这样:

Type: 1 Quantity: 1
Type: 2 Quantity: 1
Type: 3 Quantity: 1
Type: 4 Quantity: 1

示例: QR Scanner

我使用 CodeTwo Desktop QR reader 扫描二维码。

已编辑: 如果我使用 '\n':

for ($x = 0; $x < $b; $x++) 
{   
 $q .= $data[$x];
 $r .= $data2[$x];
 $s .='Type: '.$q[$x] .' Quantity: '.$r[$x];
 echo '\n';
} 

Result of '\n'

尝试使用 <br /> 标签

for ($x = 0; $x < $b; $x++) 
{   
    $q .= $data[$x];
    $r .= $data2[$x];
    $s .='Type: '.$q[$x] .' Quantity: '.$r[$x];
    echo "<br />";
} 

你的输出是 HTML 但如果你输入 plain text 那么 \n 将像下面的那样工作

<?php
header('Content-type: text/plain');
echo "abc\nxyz";
?>

记住 \n 会像这样 "\n" 而不是 '\n' 在双引号内工作。因为双引号评估变量和其他特殊字符。

当你在浏览器上运行你的PHP时,默认情况下它会呈现HTML非纯文本,你需要指定内容类型。所以在你的情况下 \n 将不起作用。

为您编辑的解决方案:

for ($x = 0; $x < $b; $x++) 
{   
 $q .= $data[$x];
 $r .= $data2[$x];
 $s .='Type: '.$q[$x] .' Quantity: '.$r[$x];
 echo "<br />"; //because your output page is html type not plain text type
} 

如果您将页面内容类型设置为 header('Content-type: text/plain');

for ($x = 0; $x < $b; $x++) 
{   
 $q .= $data[$x];
 $r .= $data2[$x];
 $s .='Type: '.$q[$x] .' Quantity: '.$r[$x];
 echo "\n"; //for content type text/plain
} 

注意:在你的result snapshot中,你的问题是这样的,但是使用"<br />"因为html内容类型

echo '\n'; <----------- Will be treated as simply a string that contains \n
echo "\n"; <----------- Will be treated as a line break, not string

在 google-in 几天后,我找到了以下答案:

for ($x = 0; $x < $b; $x++) 
{   
$q .= $data[$x];
$r .= $data2[$x];
$s .='Type: '.$q[$x] .' Quantity: '.$r[$x].'%0A';
} 

简化%0A