php string validation for basiclatin and 1supplement

怎甘沉沦 提交于 2021-01-28 02:00:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!