Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组中是否有显示换行符的方法?_Php_Mysql - Fatal编程技术网

PHP数组中是否有显示换行符的方法?

PHP数组中是否有显示换行符的方法?,php,mysql,Php,Mysql,我有一个index.php文件和一个包含消息的数组。有没有一种方法可以代替标记,在PHP中用一行新行显示文本,这样就可以将其存储在数据库中 守则: $array = array( array( 'id' => 1, 'message' => 'I\'m reading Harry Potter!', ), array( 'id' => 2, 'message' => 'Ok. I just got a notification

我有一个index.php文件和一个包含消息的数组。有没有一种方法可以代替标记,在PHP中用一行新行显示文本,这样就可以将其存储在数据库中

守则:

$array = array(
  array(
    'id' => 1,
    'message' => 'I\'m reading Harry Potter!',
  ),
  array(
    'id' => 2,
    'message' => 'Ok. I just got a notification that you sent me a pin on Pinterest.<br>Will you come to school tomorrow?',
  )
);
例如:

嗯。我刚接到通知说你给我发了一个Pinterest的pin。 你明天来学校吗

新行字符为\n。只需替换为\n,您就可以得到所需的结果

笔记 php不处理单引号内的转义字符

“\n”未作为新行字符处理,而\n是

根据您的平台,您可能需要更具体地说明您选择的新行序列


您可以关闭php标记并在中断后重新打开

例如—

white-space: pre;
有没有一种方法可以在PHP中用新行显示文本而不是标记

是的,你可以用CSS轻松地做到这一点

<p style="white-space:pre;">
   <?php echo $something; ?>
</p>
回到以前,我总是做整个替换的事情,然后我就厌倦了。现在我只使用CSS

pre选项/设置将保留空白,就像使用标记一样。唯一需要注意的是源代码中的缩进

代码中的额外空间将添加到PHP输出中,请执行以下操作:


不,我不去上学。但是您可以使用\n或任何您想要的组合。说€€€€€|÷°™{¶{¥表示换行符。在这个模式下,换行符会爆炸,您就有了自己的换行符。只需使用\n\n,然后当您想显示它时,使用nl2br。您的问题似乎与DB存储有关,但我看不到任何处理DB的代码。您在MySQL中插入换行符时有实际问题吗?@Alessio Cantarella您删除了一个换行符从改变其含义的问题中删除!@Dharman他删除了一个错误地放置在预期输出中的换行符。您现在看到的是正确的。一个be标记生成一个换行符,而不是原来的两个。这是一个非常不公平、丑陋的解决方法this@JoSSte它是如何解决任何问题的?它是PHP中的一个字符串,不能逃避PHP转换为HTML,并希望结果神奇地出现在字符串内部。使用双引号,然后使用转义序列或其他占位符等解决方案更为优雅。使用?>跳入和跳出PHP,当我使用时,它也会随之显示。例如:好的。我刚收到通知,您在Pinte上给我发送了一个pin休息。\n你明天来学校吗?带双引号的答案是一样的。请查看对我答案的编辑,并以这种方式进行测试。这很迂腐,但nl2br可以处理所有行尾\r\n\r\n\r\n我倾向于在我的代码中使用PHP\u EOL,因为它保留当前环境/平台(如Windows或OSX)的行尾。要显示消息ge…我使用了一个模板文件.php,并在index.php中使用了一个循环来包含它,所以我可以在没有echo的情况下完成它吗?是否有其他方法来编写它..?当然可以!可以很好地工作。
'message'=>'Ok.I just got a notification that you sent me a pin on Pinterest. ?>
<br>  
<?php Will you come to school tomorrow?', 
white-space: pre;
<p style="white-space:pre;">
   <?php echo $something; ?>
</p>
<p style="white-space:pre;"><?php echo $something; ?></p>