str_replace problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

str_replace problem

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: str_replace problem

Post by requinix »

Um, side note: you know you can't remove all the spaces, right?
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: str_replace problem

Post by someguyhere »

tasairis wrote:Um, side note: you know you can't remove all the spaces, right?
Why not?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: str_replace problem

Post 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?
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: str_replace problem

Post by s992 »

For some reason, I never had much luck with str_replace on newlines. You might have better luck using preg_replace.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: str_replace problem

Post by requinix »

There are three versions of a "newline" combining two characters: \r (Mac), \n (Unix/Linux), and \r\n (Windows).
agriz
Forum Contributor
Posts: 106
Joined: Sun Nov 23, 2008 9:29 pm

Re: str_replace problem

Post 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 ""
Post Reply