I want to write a regex in php to match only any english characters, spaces, numbers and all special chars.
From this question Regex any ascii character
I tried this
preg_match("/[\x00-\x7F]+/", $str);
but it throws a warning
No ending delimiter '/' found
so, how to write this regex in php.
the alternative would be smth like [a-z\d\s] and also one by one consider all special chars, but is not there a way to do simpler ?
Thanks
There are a number of intricate solutions, but I would suggest this beautifully simple regex:
^[ -~]+$
It allows all printable characters in the ASCII table.
In PHP:
$regex = '%^[ -~]+$%';
if (preg_match($regex, $yourstring, $m)) {
$thematch = $m[0];
}
else { // no match...
}
Explanation
^
anchor asserts that we are at the beginning of the string[ -~]
matches all characters between the space and the tilde (all the printable characters in the ASCII table)+
quantifier means "match one or more of those"$
anchor asserts that we are at the end of the string