我正在为读者创建一个帖子区域,我的问题是,当用户插入新帖子时,它将以这种形式保存到数据库中:
Some say the world will end in fire,<br /> Some say in ice.<br /> From what I’ve tasted of desire<br /> I hold with those who favor fire.<br /> But if it had to perish twice,<br /> I think I know enough of hate<br /> To say that for destruction ice<br /> Is also great<br /> And would suffice.
我希望这样保存它:
Some say the world will end in fire,<br />
Some say in ice.<br />
From what I’ve tasted of desire<br />
I hold with those who favor fire.<br />
But if it had to perish twice,<br />
I think I know enough of hate<br />
To say that for destruction ice<br />
Is also great<br />
And would suffice.
我希望它在每次换行后都保留标签(用于编辑问题)“输入”
在插入数据库之前,这是我当前的发布文本参数
$body = strip_tags($body);
$body = mysqli_real_escape_string($this->con, $body);
$body = str_replace('\r\n', "\n", $body);
$body = nl2br($body);
答案 0 :(得分:0)
我认为您要保存的文本不包含任何\ r \ n,这就是为什么将其保存在一行中的原因。然后您尝试搜索并替换相同的内容。
尝试搜索
,然后在\ n后面添加新行,如下所示:
str_replace('<br />', "<br />\n", $body);
我希望这项工作。
答案 1 :(得分:0)
我通过提交帖子后自动从数据库中调用body函数,然后按如下所示应用str_replace
和nl2br
来解决此问题:
$returned_ip = mysqli_insert_id($this->con);
$body= str_replace("<br />","\n" , $body);
$body= nl2br($body);
$update_query = mysqli_query($this->con, "UPDATE poems SET poem_body='$body' WHERE ip='$returned_ip'");