如何验证字符串是否包含+
或-
??
preg_match('[\-\+]', '(292+3)*1', $match);
print_r($match);
preg_match('[\-\+]', '(292-3)*1', $match);
print_r($match);
Array
(
)
Array
(
)
答案 0 :(得分:2)
正则表达式必须在分隔符之间:
preg_match('/[-+]/', '(292+3)*1', $match);
答案 1 :(得分:2)
你可以在没有正则表达式的情况下做到这一点。
$str = '+123-';
if (strpos($str, '+') !== FALSE || strpos($str, '-') !== FALSE) {
echo 'Found it';
} else {
echo 'Not found.';
}
希望这有帮助。