没有获得与 php 中存储在 mysql 数据库中相同的格式


Not getting same format as stored in mysql database in php

我在数据库中存储了一些数据:-

  main()
      {
           int num;
           printf("please enter a number");
      }

获取此字段后,当我使用 nl2br() 函数时,我想要相同的格式,但它显示如下:-

main()
{
int num;
printf("please enter a number");
}

即不占用空格。请帮助我..谢谢

HTML将跳过多个空格和换行符。虽然你是nl2br的一种方式,但你可以简单地在一组<pre>中输出整个东西,然后输出'标签,这将完全按照数据库中的方式显示它。

一个例子是这样做:

$string='Some funky
formatting with spaces and
    some additional indenting...";
echo "<pre>".$string."</pre>";

输出将是:

Some funky
formatting with spaces and
    some additional indenting...

在网页上打印多个空格字符时,仅显示一个空格。

要保留空格,您需要执行以下操作:

echo nl2br(str_replace(" ", "&nbsp;", $content));

使用 <pre> 标签(预先格式化),该标签将使用单色行距字体并保留所有空格

<pre>
text goes here and here 
         and here and here            Some out here
</pre>