0

I want to make a regex, that matches letters from all alphabets and special characters, and ignores symbols like ✪❀✱↴♜ and so on.

My regular expression looks like: /^([\p{L}0-9~!@#$%^&*_\-+='|{}\(\)\[\]:;"<>,.?\/\\\s])+$/i. Problem is - it works perfectly in regex online tester, but somehow fails in PHP code for non latin strings like ǺǻαάǠẮắẰằẳẴẵĪäÅÀÁÂåãâàáÃᗩ:

$text = 'ǺǻαάǠẮắẰằẳẴẵĪäÅÀÁÂåãâàáÃᗩ';
if(preg_match("/^([\p{L}0-9~!@#$%^&*_\-+='|{}\(\)\[\]:;\"<>,.?\/\\\s])+$/i", $text)){
    echo 'success';
} else {
    echo 'fail';
}

I have also tried another regex: /[\p{L}0-9~!@#$%^&*_\-+='|{}\(\)\[\]:;"<>,.?\/\\\s]+/i. It also works fine in regex tester, but in preg match function it matches almost everything. Does anyone know why those regular expressions behave so in preg_match function?

  • Use `/u` modifier. – Wiktor Stribiżew Aug 30 '19 at 13:47
  • Add delimiters at the start of your pattern: #/^([\p{L}0-9~!@#$%^&*_\-+='|{}\(\)\[\]:;\"<>,.?\/\\\s])+$#, also, I dont think that you can add flags with the pattern, they should be passed as a separate parametar in preg_match function – failedCoder Aug 30 '19 at 13:49

0 Answers0