Wrote a regular expression for php if( preg_match("/[Кк]ра[юй]/",$name)); I wanted to make it so that it skipped the lines "Edge", "Edge", "edge" and "edge", but as a result, for some reason, it skips all the words beginning on the edge or Kra, which has any 4th letter. Those. skips and crab. Please tell me why the last condition [юй] , does not work as it should, skipping words not only with st and th, but also with other letters?
- And if so '/ [Kk] {1} ra [yu] {1} /'? - Alekcvp
- Vseravno skips - vlad3243
- one@ vlad3243, you do not forget about 2 things: 1) fix the beginning and end of the line 2) The [Q] construction will skip K, skip to and skip the empty string. - Victor Evlampiev
- @Viktor Evlampyev, the first is correct, the second is not. - Visman am
- preg_match ("/ [^ Kk (paradise | paradise)] / i", $ name, $ matches); print_r ($ matches); poprobuyte eto - Vanya Avchyan
|
3 answers
Apparently, you are working with utf8 . To work correctly with Unicode for regular expressions in PHP, you must specify the modifier u (not U , but u )
melkij@melkij:~$ php -r 'var_dump(preg_match("/[Кк]ра[юй]/","краб"));' int(1) melkij@melkij:~$ php -r 'var_dump(preg_match("/[Кк]ра[юй]/u","краб"));' int(0) melkij@melkij:~$ php -r 'var_dump(preg_match("/[Кк]ра[юй]/u","край"));' int(1) Without this modifier, the character mask treats 2-byte Cyrillic codes as two different characters and therefore will not work normally.
|
True regex, it is not clear why it does not work, try this: "# [to] ra [yu] #i"
Also complete the sample contents of the $ name variable.
- Your skip edge and edge, and this is not necessary to the author, as I understood. - Alekcvp
- misunderstood, he needs to not miss the "crab" :) - gibigate
|
Now, if I need only 4 variants, I would most likely have written such a construction
if( strpos($message,'Край') !== false || strpos($message,'край') !== false || strpos($message,'краю') !== false || strpos($message,'Краю') !== false ) { echo 'true'; } |