删除 PHP 中的换行符 - 但它们仍然显示在 MySQL 的 LONGTEXT 中
你们好,伙计们。
好的 - 像这样清理我的字符串:
$clean_string = preg_replace("/[\n\r]/"," ",trim($dirty_string));
..将它回显到屏幕上并且它起作用了 - 一大块可爱的文本,没有换行符。
将其插入 MySQL 中的 LONGTEXT 类型字段 - 然后在 Sequel Pro 中预览结果数据 - 由于某种原因它有新行。有很多,就像它在我清理之前的新的时候一样。
我做错了什么?是长文本吗?
这就是当我使用 SO 页面的内容作为字符串时 html 源的样子 - 请注意许多空格和换行符 - 即使已清理!
mysql - Trouble with CONCAT and Longtext - Stack Overflow
Stack Exchange
Ello chaps.
Ok - cleaning my string like this:
$clean_string = preg_replace("/[\n\r]/"," ",trim($dirty_string));
.. Echo it out to the screen and it works - one lovely big lump of text with no newlines.
INSERT it into a LONGTEXT type field in MySQL - then preview the resulting data in Sequel Pro - and for some reason it has new lines. Loads of them, just like it did when it was new, before I cleaned it.
What am I doing wrong? Is it LONGTEXT?
This is what the html source looks like when I use the content of a SO page as a the string - note the many spaces and newlines - even when cleaned!
mysql - Trouble with CONCAT and Longtext - Stack Overflow
Stack Exchange
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过使用以下方法解决:
但是 - 将双引号替换为单引号。现在可以工作了。
感谢mfonda让我接近!
Solved by using this:
BUT - replacing double quotes for single quotes. Works now.
Thanks mfonda for getting me close!
那应该有效。另外,这里确实不需要使用 preg_replace -
$clean_str = str_replace(array("\r", "\n"), '', $dirty_string)
就可以正常工作,并且会快一点。That should work. Also there really isn't any need to use preg_replace here--
$clean_str = str_replace(array("\r", "\n"), '', $dirty_string)
will work just fine, and will be a little faster.$clean_string = preg_replace('/\s+/m', ' ',trim($string));
会将每个“空格序列”替换为单个空格。尝试一下,看看它是否按预期工作。$clean_string = preg_replace('/\s+/m', ' ',trim($string));
will replace every "space sequence" into a single space. Try it and see if it works as expected.删除所有换行符后进行修剪怎么样?
How about trimming AFTER removing all newlines?