3

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
a_good_swan
  • 65
  • 1
  • 1
  • 10
  • May be you still see old records. Are you sure that the table is empty before the insert? – Ivan Buttinoni Jan 11 '11 at 18:13
  • When you say "echo it out to the screen", are you referring to a browser window? If so, are there any linebreaks in the HTML source? – lonesomeday Jan 11 '11 at 18:14
  • @Ivan - nope, refreshed and fresh inserts. – a_good_swan Jan 11 '11 at 18:17
  • @lonesomeday - Looked at the source, no line breaks but lots of spaces - could this be an invisible line break character that's NOT \n or \r ? – a_good_swan Jan 11 '11 at 18:17
  • @evilswan I can exclude the type of the column, so it's a bug in your code, at the moment I can image 2 possibilities: you clean the string but you insert the dirty one, or you fail with the regexp: use the str_replace of "mfonda" – Ivan Buttinoni Jan 11 '11 at 18:43
  • Did you check if your query even succeeded? `mysql_query()` returns false if the query failed. If it looks like the DB's not changing between updates, then check if the updates are even happening. – Marc B Jan 11 '11 at 19:42
  • @Marc these are new rows and do succeed. Thanks for the idea though. – a_good_swan Jan 11 '11 at 20:04
  • Added a copy/paste of the resulting HTML output so you can see all the newlines still there. – a_good_swan Jan 12 '11 at 11:40

4 Answers4

2

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!

a_good_swan
  • 65
  • 1
  • 1
  • 10
1

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.

mfonda
  • 7,873
  • 1
  • 26
  • 30
0

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

Xavier Barbosa
  • 3,919
  • 1
  • 20
  • 18
  • Resulting string when previewed in Sequel Pro still has newlines. :( Could it be 'tab' characters that are being shown!? – a_good_swan Jan 12 '11 at 14:20
  • /s+ covers all space characters, including tabs. Maybe you're not actually inserting the correct string into your database, or an issue in the viewing program? – mfonda Jan 12 '11 at 17:38
0

How about trimming AFTER removing all newlines?

$clean_str = trim(str_replace(array("\r", "\n"), '', $dirty_string));
DaDummy
  • 64
  • 3