Function to return only alpha-numeric characters from string ?

Function to return only alpha-numeric characters from string ?

Asked on January 11, 2019 in Regex.
Add Comment


  • 1 Answer(s)

    Use below code to remove everything except a-z, A-Z and 0-9:

    $result = preg_replace("/[^a-zA-Z0-9]+/", "", $s);
    
    

    Definition:

    Alphanumeric includes letters in foreign languages and obsolete scripts and  use the Unicode character classes.

    use this to leave only A-Z:

    $result = preg_replace("/[^A-Z]+/", "", $s);
    
    

    The words like résumé contains the letter é that not be matched by this.To match a specific list of letters adjust the regular expression to include those letters. To match all letters, use the appropriate character classes.

    Answered on January 11, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.