Allow only [a-z][A-Z][0-9] in string using PHP

How can I get a string that only contains a to z, A to Z, 0 to 9 and some symbols?


Solution 1:

You can filter it like:

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

As for some symbols, you should be more specific

Solution 2:

You can test your string (let $str) using preg_match:

if(preg_match("/^[a-zA-Z0-9]+$/", $str) == 1) {
    // string only contain the a to z , A to Z, 0 to 9
}

If you need more symbols you can add them before ]