PDA

View Full Version : php replace function



james_byrne
07-14-2008, 05:53 AM
I would like to replace " in php, when i wrote my replace function to replace \" and it almost works, but the text i am replaceing it with " comes out as \" so i am obviously missing something, i do not want to also replace all \ so can you please help me.

Thanks in advance for the help.

BTW here my code with the outputs.



$content = $_POST['content']'

$replace_me = array("\"","&", "\'");
$replace = array(""","&", "'");

$newphrase = str_replace($replace_me, $replace, $content);


and if i type a " in the box that gets posted as content i get out.

\"

dicegame
07-14-2008, 07:49 AM
Try:



<?php
$content = $_POST['content'];

$replace_me = array('\"',"&", "\'");
$replace = array("&quot;","&amp;", "&apos;");

$newphrase = str_replace($replace_me, $replace, $content);

?>

boogyman
07-14-2008, 12:42 PM
research addslashes (http://www.php.net/addslashes)

james_byrne
07-15-2008, 04:44 AM
Thanks the '\"' worked great. Thanks.

dicegame
07-15-2008, 11:04 PM
Just a quick note

"\""

Does not work as you expect because when you use \ what it means is that it escapes the next ". whenever you want to cancel out something like what you did and you've got the same quoatations e.g. "\"" or '\'' that won't work... you will need to switch the outside quotes around.

james_byrne
07-16-2008, 06:11 PM
Thanks that makes sence.