Page 1 of 1

str_replace problem

Posted: Sun Nov 14, 2010 6:44 pm
by someguyhere
I'm having a problem getting str_replace to work properly in the following code:

Code: Select all

$un_comp = array(" ", "\n", ";}");
$comp = array("", "", "}");
$css = str_replace($un_comp, $comp, $_POST["css"]); 
echo $css;
For some reason, it's not striping out the spaces or line breaks. What am I doing wrong?

Re: str_replace problem

Posted: Sun Nov 14, 2010 7:04 pm
by requinix
Um, side note: you know you can't remove all the spaces, right?

Re: str_replace problem

Posted: Sun Nov 14, 2010 7:56 pm
by someguyhere
tasairis wrote:Um, side note: you know you can't remove all the spaces, right?
Why not?

Re: str_replace problem

Posted: Sun Nov 14, 2010 8:11 pm
by Celauran
someguyhere wrote:
tasairis wrote:Um, side note: you know you can't remove all the spaces, right?
Why not?

Code: Select all

.foo
{
  border: 1px solid #000;
}
That aside, I can't duplicate the problem. Can you post an example of what isn't working?

Re: str_replace problem

Posted: Sun Nov 14, 2010 11:44 pm
by s992
For some reason, I never had much luck with str_replace on newlines. You might have better luck using preg_replace.

Re: str_replace problem

Posted: Mon Nov 15, 2010 12:08 am
by requinix
There are three versions of a "newline" combining two characters: \r (Mac), \n (Unix/Linux), and \r\n (Windows).

Re: str_replace problem

Posted: Mon Nov 15, 2010 1:23 am
by agriz
For new line, if it is not working, It might help.

Convert all the new line to break

Code: Select all

nl2br
Then replace <br /> to ""