Need to check for the presence in the character string. The string must contain only Cyrillic characters, Latin characters, spaces and nothing else. I tried to create a regular schedule in js, but for some reason it works completely wrong!

if (preg_match("/[^A-zА-я]/","daksks sdsd ывыв1234234") == 0) { echo "Да"; } 

2 answers 2

 preg_match( "/[^a-zа-яё ]/iu", $text ); 

Only Cyrillic, Latin and space.

  • In my opinion, there is not enough space in square brackets? - cheops
  • Yes, thanks, I missed - ReinRaus
  • @ReinRaus and capital letters it will also be considered valid ??? - dantelol
  • @dantelol, for this and there is a flag \i . - VenZell
  • @ReinRaus so? / [^ `a-za-ya] / iu - dantelol

You can use the following option

 <?php if (!preg_match("/[^\w\d\s]/u", "daksks sdsd ывыв1234234")) { echo "Да"; } 

The u modifier is used for UTF8 text. If the classes \ w, \ d and \ s are too general for you, you can use this expression

 <?php if (!preg_match("/[^0-9a-zA-Zа-яёА-ЯЁ ]/u", "daksks sdsd ывыв1234234")) { echo "Да"; } 

The above expressions take into account the numbers, if they need to be deleted, then the expression should be corrected "/[^\w\s]/u" and "/[^a-zA-Zа-яёА-ЯЁ ]/u" , respectively.

  • Skip numbers, line breaks, horizontal and vertical tabs. - ReinRaus
  • Thank you, added a comment. - cheops