开发者

Removing newlines in PHP - but they still show in LONGTEXT in MySQL

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 w开发者_JAVA技巧as 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


Solved by using this:

$clean_str = str_replace(array("\r", "\n"), '', $dirty_string);

BUT - replacing double quotes for single quotes. Works now.

$clean_str = str_replace(array('\r', '\n'), '', $dirty_string);

Thanks mfonda for getting me close!


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)); 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?

$clean_str = trim(str_replace(array("\r", "\n"), '', $dirty_string));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜