如果字符串包含+或-,则preg_match


preg_match if string contain + or -

如何验证字符串是否包含+-??

preg_match('['-'+]', '(292+3)*1', $match);
print_r($match);
preg_match('['-'+]', '(292-3)*1', $match);
print_r($match);

输出

Array
(
)
Array
(
)

Regex必须位于分隔符之间:

preg_match('/[-+]/', '(292+3)*1', $match);

您可以在没有regex的情况下完成此操作。

使用strpos()函数。

$str = '+123-';
if (strpos($str, '+') !== FALSE || strpos($str, '-') !== FALSE) {
     echo 'Found it';
} else {
     echo 'Not found.';
}

希望这能有所帮助。