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
someguyhere
Forum Contributor
Posts: 181 Joined: Sun Jul 27, 2008 3:24 pm
Post
by someguyhere » Sun Nov 14, 2010 6:44 pm
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?
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sun Nov 14, 2010 7:04 pm
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
Post
by someguyhere » Sun Nov 14, 2010 7:56 pm
tasairis wrote: Um, side note: you know you can't remove all the spaces, right?
Why not?
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Sun Nov 14, 2010 8:11 pm
someguyhere wrote: tasairis wrote: Um, side note: you know you can't remove all the spaces, right?
Why not?
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
Post
by s992 » Sun Nov 14, 2010 11:44 pm
For some reason, I never had much luck with str_replace on newlines. You might have better luck using preg_replace.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Mon Nov 15, 2010 12:08 am
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
Post
by agriz » Mon Nov 15, 2010 1:23 am
For new line, if it is not working, It might help.
Convert all the new line to break
Then replace <br /> to ""