当前位置: 动力学知识库 > 问答 > 编程问答 >

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 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

网友答案:

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.

网友答案:

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!

网友答案:

$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));
分享给朋友:
您可能感兴趣的文章:
随机阅读: