Haradzieniec
Haradzieniec

Reputation: 9340

preg_match replace ALL condition with ANY

Seems like

preg_match('/^[\p{Cyrillic}]+$/', $str)

returns 0 or 1 based on if $str contains ALL Cyrillic letters.

I need 0 or 1 based on if $str contains ANY Cyrillic letters.

Thank you.

Upvotes: 2

Views: 86

Answers (2)

anubhava
anubhava

Reputation: 785146

You can use:

$ret = preg_match('/\p{Cyrillic}/u', $str);

to figure if input string contains any Cyrillic character or not. /u flag is required to handle unicode string inputs.

Alternatively use mb_ereg function for multibyte regex match like this:

$str = 'БДКЯ'; // string with Cyrillic characters only

// check with Cyrillic string only
var_dump( mb_ereg('\p{Cyrillic}', $str) ); // int(1)

// check with mix of Cyrillic and ASCII characters
var_dump( mb_ereg('\p{Cyrillic}', $str . 'abc') ); // int(1)

// check with ASCII characters only
var_dump( mb_ereg('\p{Cyrillic}', 'abc') ); // bool(false)

Upvotes: 1

AbraCadaver
AbraCadaver

Reputation: 78994

The anchors ^ and $ force the {Cyrillic} character match from the beginning to the end of the string, so remove them. Also, the character class [] and + are not needed because you are looking for any match:

 /\p{Cyrillic}/

Upvotes: 1

Related Questions