dongluo3962 2011-03-31 21:46
浏览 57
已采纳

从MySql数据库中提取文本并将其拆分为网站上的段落

Can I just insert formated text into database and retrieve it as that? Should formating ( like new line ) appear on website after pulled from database? I have tried it and it seems that is not case.

How to accomplish that?

Thanks

  • 写回答

2条回答 默认 最新

  • doushao5047 2011-03-31 21:51
    关注

    If you want it to be formatted as is inserted in the database you could output it to a <pre> tag to preserve formatting. If you just want to have the newline replaced by a html tag, you can do that easily:

    <?php
    
    //be on the safe side, as suggested by Christian Sciberras
    $sanitized = htmlspecialchars($databaseContent, ENT_QUOTES);
    
    echo str_replace(array("
    ", "
    "), array("<br />", "<br />"), $sanitized);
    

    Why?

    I use str_replace() in this case to show you how it's done. I usually don't replace the new line with a break but with a </p><p> (and this is why I didn't go for a nl2br() solution) If you think you'd want paragraphs instead of line breaks, just change the replacement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画