Forum Moderators: coopster

Message Too Old, No Replies

PHP Whitespace Removal for RSS

         

username

7:15 am on Oct 13, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi,

I am pulling some text from a MySQL DB field, and displaying in an RSS Feed. Single paragraph content works fine, but multi paragraph variables are causing the error "Whitespace is not allowed at this location" in the RSS file. How do I remove the whitespace from the string?

Example:

$var = "one two

three";

I have tried regular expressions and str_replace. No luck so far.

Thanks in advance.

andrewsmd

1:36 pm on Oct 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what did you try str_replace on? did you try it on a \n?

rocknbil

8:49 pm on Oct 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$var = preg_replace('/[\n\r]+/',' ',$var);

username

9:02 pm on Oct 13, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hmm, strangely enough, it produces the same error in a different place in the document with rocknbil's code added. Any ideas?

username

6:24 am on Oct 14, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



So, any ideas?

TheMadScientist

9:33 am on Oct 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$var = preg_replace('/[\n\r\t]+/',' ',$var);

Is about the only guess I have...

andrewsmd

7:45 pm on Oct 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've always had strange results with the regex replace functions try using three str replace functions one for each. I know it's more code, but I tend to get more reliable results with them.

TheMadScientist

9:32 pm on Oct 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To expand on andrewsmd's idea, you can actually feed arrays into str_replace:

$WhiteSpace = array("\r\n", "\n", "\r", "\t");
$ReplaceWith = " ";
$NewStr = str_replace($WhiteSpace, $ReplaceWith, $String);