Replace characters in variable

Discussion in 'PHP' started by GLD, Nov 11, 2006.

  1. #1
    I have a variable which could possibly, but not necessarily, contain a ' character (single quote).

    I have to replace any single quotes in this variable with \' (so as to escape the single quote when I use the variable in an html tag). How can I do this?

    I've looked into preg_replace, but it's all a bit complicated, and I can't seem to quite get it right.

    Thanks in advance, :)
     
    GLD, Nov 11, 2006 IP
  2. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #2
    $new_string = addslashes($old_string);
     
    SoKickIt, Nov 11, 2006 IP
    GLD likes this.
  3. GLD

    GLD Well-Known Member

    Messages:
    307
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Perfect - thanks.
    Repped!
     
    GLD, Nov 11, 2006 IP