duanjia4220 2014-04-09 01:52
浏览 43
已采纳

拿SQL段落和空行(PHP,CSS,$ MySQL)

I have been scanning through posts here for a bit and while I found quite a bit of it useful, I was unable to find all of my questions. More or less I am coding a website with a back-end that allows the user (me) to post stories that are then saved to the MySQL DB (UTF-8) for later use. I then on the front-end have a page that displays said story with attempts to bring back in the P tags and BR tags to make the format look nice.

Upon saving the stories content I used an "escape" function to turn all HTML tags the user might enter into part of a string rather than a code. I have figured out a way, however sloppy it may be, to bring back

tags with the following function:

function set_pars($string) {
   return "<p>" . str_replace("
", "</p><p>", $string) . "</p>";
}

Seeing as that there is no at the beginning of the string and no at the end (if I am even understanding these two characters correctly) I was forced to add them at the beginning and the end. I have yet to find a way to add in the BR for returned blank lines. I thought I was being brilliant for a brief moment when I set the P tag to "height: 20px;" until I had a paragraph that was longer than one line... I am sure someone might get a good laugh out of that one.

The reason I have chosen to do the styling after the fact, rather than adding them to the DB directly, is so I don't have to see the ugly tag mock-up while working with a story (though as I am writing this it has occurred to me I could remove them before filling the retrieved story into an TEXTAREA feild. Seems like unnecessary back and forth though.) What ever helps me to understand and make this work! Thank you in advanced for any info anyone can shed on this topic.

In case my ramblings have gone too far past the point I want to make this:

Hello I am the first line.

Hello I am the second line.

Look like this:

<p>Hello I am the first line.</p>
<br />
<p>Hello I am the second line.</p>

This is the final function I ended up using and it works perfectly:

function set_pars($string) {
$string = str_replace(array("
","
",""), array("<br />","<p>","</p>"), $string);
return "<p>" . $string . "</p>";

}

  • 写回答

1条回答 默认 最新

  • douxidao3524 2014-04-09 02:11
    关注

    Replace the first with </p><br /><p> and then on the resulting string, do the replacement the way you are doing now.

    When you hit enter twice to create a blank line in between the two, just the are repeated twice.

    Hello I am the first line.
    
    Hello I am the second line.
    

    Above results in Hello I am the first line. Hello I am the second line..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图