I'm trying to write a PHP script for some form validation. One of the requirements for a text input is that it must contain at least one unicode character. I wrote this function to check for this.
function containsLetters($str)
{
return preg_match('/\p{L&}+/', $str);
}
It seemed to work fine for the first couple of test cases I wrote for it. But then I tried to test it against the string " ", expecting a false result. Instead, it seems to completely crash.
I tried out this as well:
var $test = preg_match('/\p{L&}+/', "
");
var_dump($test);
which also doesn't run.
In the second attempt, you're trying to assign the call to a variable, don't ...
var_dump(preg_match('/\p{L&}/', "
")); // int(0)
Also, you can just use \pL
instead and be sure to enable the u
(unicode) modifier ...
function containsLetters($str) {
return (bool) preg_match('~\pL~u', $str);
}