Regex won't accept an apostrophe

Discussion in 'PHP' started by patter, Sep 19, 2010.

  1. #1
    I'm trying to use preg_replace to remove everytihng from a string except letters and the apostrophe. The following code results in whos, instead of who's. Would someone please explain my mistake?
    
    $text = "who's";
    $text = preg_replace("/[^a-zA-Z']/", "", $text);
    
    PHP:

     
    patter, Sep 19, 2010 IP
  2. WWSD

    WWSD Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try escaping the apos with backslash \ so
    $text = "who's";
    $text = preg_replace("/[^a-zA-Z\']/", "", $text);
     
    WWSD, Sep 19, 2010 IP
  3. Chuckun

    Chuckun Well-Known Member

    Messages:
    1,161
    Likes Received:
    60
    Best Answers:
    2
    Trophy Points:
    150
    #3
    If you were only stripping them for security or something, then use addslashes() and stripslashes()..

    $text = "who's";
    $text = addslashes($text);  // turns "who's" into "who\'s"
    $text = preg_replace("/[^a-zA-Z\]/", "", $text);
    
    PHP:
    then echo stripslashes($text); and it'll return "who's" without breaking any code.
     
    Chuckun, Sep 19, 2010 IP
  4. patter

    patter Member

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    I tried this but it still strips the apostrophe.
     
    patter, Sep 19, 2010 IP
  5. patter

    patter Member

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    No, it's not for security. The original text can contain any character but the useable (resulting) text can only contain letters and the apostrophe since, without it, the word is incorrect. I tried your example though but it failed with "Compilation failed: missing terminating ]." I assume you meant [^a-zA-Z\!] so I tried that but the result is the same - the apostrophe gets removed.
     
    patter, Sep 19, 2010 IP
  6. WWSD

    WWSD Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    oops my mistake. I was confusing the ` (apostrophe) symbol with the single quote ( ' ) So this does work

    $text = "who's";
    $text = preg_replace("/[^a-zA-Z\']/", "", $text);
    echo $text;

    But if you are also confusing the two, and there is another problem then lemme know
     
    WWSD, Sep 19, 2010 IP
  7. patter

    patter Member

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    The actual problem turned out to be how the text was stored in the database. The apostrophe was being stored as &#39 instead of '. So I add this code and everything is working now
    $text = str_replace("&#39", "'", $text);
    PHP:
    I appreciate the help. It showed me my initial assumption was incorrect and allowed me to get to the actaul problem. Thank you both. :)
     
    Last edited: Sep 21, 2010
    patter, Sep 21, 2010 IP
  8. Chuckun

    Chuckun Well-Known Member

    Messages:
    1,161
    Likes Received:
    60
    Best Answers:
    2
    Trophy Points:
    150
    #8
    Glad you figured it out :)

    Often the input of other people (even if it's incorrect input) can help your mind stumble upon the right answer! :)

    Good luck with the rest! :)

    Chuckun
     
    Chuckun, Sep 21, 2010 IP