Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: RegEx vs. ctype_*

several months ago, you provided a perfect solution for my IsAlpha & co issues. But once again, I ran into issues after upgrading PHP (to version 5.2.9), although the ctype_* functions seem to do their job now:

ctype_alpha( $value ) /* VS */ preg_match("/^[\p{L} _.\-]+$/u",    $value)

ctype_alnum( $value ) /* VS */ preg_match("/^[\p{L}0-9 _.\-]+$/u", $value)

By issues, I mean that 'GB' or 'blablue' is i.e. correctly identified as alpha by ctype_alpha(), but fails with preg_match("/^[\p{L} _.\-]+$/u", $value).

Please let me know if you have any ideas, I ran out of them after some serious googling..

Many, many thanks!

P.S. LANG/LC_CTYPE/etc is set to en_US.UTF-8 both on both environments

like image 718
MrG Avatar asked Mar 08 '26 23:03

MrG


1 Answers

Make sure PCRE has been compiled with UTF-8 support as well as Unicode property support.

if ( ! @preg_match('/^.$/u', 'ñ'))
    echo 'PCRE has not been compiled with UTF-8 support.';

if ( ! @preg_match('/^\pL$/u', 'ñ'))
    echo 'PCRE has not been compiled with Unicode property support.';

Checks from http://github.com/kohana/kohana/blob/master/install.php.

like image 116
Geert Avatar answered Mar 10 '26 14:03

Geert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!