Results 1 to 6 of 6

Thread: php replace function

  1. #1
    Join Date
    Jul 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default php replace function

    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.

    Code:
    $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.

    \"

  2. #2
    Join Date
    Jul 2008
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Try:

    PHP Code:
    <?php
    $content 
    $_POST['content'];
                
    $replace_me = array('\"',"&""\'");
    $replace   = array("&quot;","&amp;""&apos;");

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

    ?>

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    research addslashes

  4. #4
    Join Date
    Jul 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks the '\"' worked great. Thanks.

  5. #5
    Join Date
    Jul 2008
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  6. #6
    Join Date
    Jul 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks that makes sence.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •