我需要检查一个字符串,以确定它是否包含除|之外的任何字符,以便为除了|之外没有任何字符的变量分配空值(理论上可以有任意数量的|字符,但可能不会超过5-6个)。像||||一样
我可以看到遍历字符串的每个字符或诸如此类的东西,但我觉得肯定有一种更简单的方法。
发布于 2012-04-20 22:26:05
if (preg_match('/[^|]/', $string)) {
// string contains characters other than |
}或者:
if (strlen(str_replace('|', '', $string)) > 0) {
// string contains characters other than |
}发布于 2012-04-20 22:27:12
可以,您可以使用正则表达式:
if(! preg_match('/[^\|]/', $string)) {
$string = NULL;
}发布于 2016-08-29 18:02:53
我想检查一个字符串是否只包含某些字符。为了防止双重否定(因为我发现它们更难读),我决定使用以下正则表达式:
preg_match('/^[|]+$/', $string)这将检查字符串从头到尾是否只包含|字符(至少一个)。
https://stackoverflow.com/questions/10248107
复制相似问题