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

Ello chaps.

Ok - clear the line as follows:

$clean_string = preg_replace("/[\n\r]/"," ",trim($dirty_string));

.. Take it to the screen and it works - one beautiful big piece of text without a new line.

INSERT it in a field like LONGTEXT in MySQL - then view the received data in Sequel Pro - and for some reason it has new lines. Loads them, as and 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 contents of an SO page as a string - pay attention to many spaces and translation strings even when clearing!

    mysql - Trouble with CONCAT and Longtext - Stack Overflow





































                Stack Exchange
+3
source share
4 answers

Decision:

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

BUT - replacing double quotes for single quotes. It works now.

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

mfonda , !

+1

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

+1

$clean_string = preg_replace('/\s+/m', ' ',trim($string));will replace each "spatial sequence" with one space. Try it and see if it works as expected.

0
source

How about cropping AFTER deleting all lines of a newline?

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

Source: https://habr.com/ru/post/1784792/


All Articles