问题
I have an xml schema where a description is defined like this:
[\p{IsBasicLatin}\p{IsLatin-1Supplement}]{1,1000}
and i have to check in PHP that the user input string is valid for this encoding. Checking for the length is easy, but i don't find a way to use preg_match to check for basic latin. I've tried:
preg_match('@^\p{IsBasicLatin}+@^\p{IsLatin-1Supplement}+$@u', $string);
but it says that there's an unknown property, even using basicLatin or Latin.
回答1:
The pattern \p{IsBasicLatin} stands for [\x00-\x7F] and \p{IsLatin-1Supplement} stands for [\x80-\xFF] (see Unicode reference).
Thus, all you need is
preg_match('~^[\x00-\xFF]{1,1000}$~u', $s)
See the PHP demo.
回答2:
This is quite simple. Just check for the existence of either string:
<?php
$regex = '#(IsBasicLatin|IsLatin)#';
$string = '[\p{IsBasicLatin}\p{IsLatin-1Supplement}]{1,1000}';
if (preg_match($regex, $string)) {
echo 'Lorem Ipsum Shipmsum flipsum';
}
Lorem Ipsum Shipmsum flipsum
See it here https://3v4l.org/P9RDu
Play with the regex here https://regex101.com/r/p56prj/1
来源:https://stackoverflow.com/questions/53043409/php-string-validation-for-basiclatin-and-1supplement