duanbicheng3345 2014-10-28 00:15
浏览 71
已采纳

使用'preg_replace_callback'时,从Textarea显示新行段落

Ok, so i had the paragraphs displaying fine when pulled out of a database. Using CSS i had the line...

white-space: pre-line;

New lines for paragraphs were displaying great.

But then i added some code to make the first letter of every sentence capitalized. The code is...

$description = htmlentities(strip_tags($_POST['desc']), ENT_QUOTES);
$desc = preg_replace_callback('/([.!?])\s*(\w)/', function ($matches) {
    return strtoupper($matches[1] . ' ' . $matches[2]);
},  ucfirst(strtolower($description)));

So although it worked it now does not render the new lines for new paragraphs. In other words it is ignoring the CSS line above or something.

Any ideas??

  • 写回答

1条回答 默认 最新

  • dsmupo6631 2014-10-28 00:43
    关注

    It's not ignoring the CSS, there's no new line anymore.

    See you are matching what you want right but you are replacing without the new line characters the first ([.!?]) and the second (\w) groups of the expression.

    Change the expression to '/([.!?])(\s*)(\w)/' then the return statement to strtoupper($matches[1] . $matches[2] . $matches[3])

    It should work.

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

报告相同问题?

悬赏问题

  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果