How to make preg_match that if there are characters in the line except letters and space then return false

by pattern

^[A-zА-ЯёЁ ] 

well, or something like that

    1 answer 1

     preg_match('/^[a-zа-яё ]+$/ui', 'ab c А бв '); 
    • and not so? ^ means the beginning of $ the end seems to be like, but here it is the negation of preg_match ('/ [^ a-za-yo] + / i', 'ab c A bv'); - dfhsfhgfj 3:04 pm
    • @dfhsfhgfj> ^ means the beginning of $ the end seems to be all right, and therefore preg_match returns false if everything except letters. $ subject = 'ab c A bv'; / * If false * / if (! Preg_match ('/ ^ [a-za-ya] + $ / ui', $ subject)) {echo 'The value must contain only alphabetic characters and a space'; } Do not forget to use the u modifier, since you can probably use Cyrillic in the string. - romeo
    • thanks for the detailed answer - dfhsfhgfj pm
    • Will this code work correctly? preg_match ('/ [^ a-za-ya] + / i', 'ab c A bv'); I did not understand, is it an analogue of what you wrote or not? - dfhsfhgfj
    • @dfhsfhgfj Yes, it will, but the value will be the opposite. preg_match ('/ ^ [^ a-za-ya] + $ / iu', '# * _ 122'); will print true if the string does not contain alphabetic characters and a space. Then the above condition will look like this: / * If true / if (preg_match ('/ ^ [^ a-za-ya] + $ / ui', $ subject)) {echo 'The value must contain only alphabetic characters and a space' ; } The characters of the beginning and end of the search string (** ^ ... $ * ) in the pattern are required. If this is not done, then the array (3 argument of the preg_match function) will get part of the string that satisfies this condition, i.e. will be returned ... - - romeo