php - 如何从字符串中删除不必要的空格,使 HTML 中没有多余的空格

标签 php html

如何从字符串中删除不必要的空格,使 HTML 中没有多余的空格??

我从数据库中获取字符串,现在我正在尝试做类似的事情:

nl2br(trim(preg_replace('/(\r?\n){3,}/', '$1$1', $comment->text)));

但它一直这样显示:

enter image description here

我需要的是变得完美:

enter image description here

这是怎么做到的??因为我不擅长正则表达式:(

编辑: $comment->text 包含来自 DB 的文本:

enter image description here

最佳答案

preg_replace('/(\r)|(\n)/', '', $comment->text);

输出

"1 2"<br>"2 3"<br>"3"<br>"4"<br>"5"

关于php - 如何从字符串中删除不必要的空格,使 HTML 中没有多余的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20683876/

相关文章:

php - 避免特定时间间隔的重复数据

jquery - 使用 jquery 但不使用 canvas 检查 .png 图像中的透明度

javascript - 将子级插入到 jQuery UI Accordion 的 DIV 中

html - 使用图像作为提交按钮

javascript - 处理网页上的所有/任何链接点击

php - mysql插入后如何最好地访问警报数据

php - 浮点测试断言 - 为什么这些 "identical"数组会失败?

php - 具有 Backbone、DELETE 和 PUT 请求的 CodeIgniter Rest Server 返回 404

javascript - Joomla 页面 : "The type attribute is unnecessary for JavaScript resources." 的 W3C 验证

javascript - ngIf else if 在 Angular 中