PHP preg_match 如果找不到文本,正则表达式会发送警告
PHP preg_match regex send warning if not find the text
我的数据库中有一个table,我想在table中获取记录并与用户输入的文本进行比较,如果匹配,则获取并显示其他字段table.
的那一行
这是我的代码:
$q = "SELECT * FROM Kala2 WHERE 1";
$res = mysqli_query($db, $q);
while ($row = mysqli_fetch_assoc($res)) //For each word
{
$t = $row[sharhe_kala];
$p = $row[mark];
if (preg_match("/$t/i", $text)) {
if (preg_match("/$p/i", $text)) {
*** do something ***
}}
它可以工作,但是当用户 ($text
) 输入的单词不在数据库中时,它会发送此警告:
PHP Warning: preg_match(): Unknown modifier '1' in
当 $row[sharhe_kala]
或 $row[mark]
是数字时也不回答
例如,如果 $t="KOYO"
和 $p="Ballbearing"
并且用户输入 $text="koyo ballbearing"
它会显示结果并且没问题,但是如果用户输入 $text="ntn zzz"
它会发送此警告:
PHP Warning: preg_match(): Unknown modifier '1' in
并且如果 $t="222"
和 $p="Ballbearing"
并且用户输入 $text="222 ballbearing"
它不会显示结果并且不会显示任何警告。
来自preg_match:
This function may return Boolean FALSE, but may also return a
non-Boolean value which evaluates to FALSE. Please read the section on
Booleans for more information. Use the === operator for testing the
return value of this function.
听起来您的变量包含特殊字符,即您用作分隔符的斜杠 (/)。尝试 preg_quote()
'ing 你的变量。例如:
if (preg_match("/".preg_quote($t, '/')."/i", $text)) {
if (preg_match("/".preg_quote($p, '/')."/i", $text)) {
您必须转义您在正则表达式中直接发送的变量。
$text = "222 ballbearing";
$t = 222;
$p = "Ballbearing";
if (preg_match("/{$t}/i", $text)) {
if (preg_match("/{$p}/i", $text)) {
echo "Match detected";
}
}
我的数据库中有一个table,我想在table中获取记录并与用户输入的文本进行比较,如果匹配,则获取并显示其他字段table.
的那一行这是我的代码:
$q = "SELECT * FROM Kala2 WHERE 1";
$res = mysqli_query($db, $q);
while ($row = mysqli_fetch_assoc($res)) //For each word
{
$t = $row[sharhe_kala];
$p = $row[mark];
if (preg_match("/$t/i", $text)) {
if (preg_match("/$p/i", $text)) {
*** do something ***
}}
它可以工作,但是当用户 ($text
) 输入的单词不在数据库中时,它会发送此警告:
PHP Warning: preg_match(): Unknown modifier '1' in
当 $row[sharhe_kala]
或 $row[mark]
是数字时也不回答
例如,如果 $t="KOYO"
和 $p="Ballbearing"
并且用户输入 $text="koyo ballbearing"
它会显示结果并且没问题,但是如果用户输入 $text="ntn zzz"
它会发送此警告:
PHP Warning: preg_match(): Unknown modifier '1' in
并且如果 $t="222"
和 $p="Ballbearing"
并且用户输入 $text="222 ballbearing"
它不会显示结果并且不会显示任何警告。
来自preg_match:
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
听起来您的变量包含特殊字符,即您用作分隔符的斜杠 (/)。尝试 preg_quote()
'ing 你的变量。例如:
if (preg_match("/".preg_quote($t, '/')."/i", $text)) {
if (preg_match("/".preg_quote($p, '/')."/i", $text)) {
您必须转义您在正则表达式中直接发送的变量。
$text = "222 ballbearing";
$t = 222;
$p = "Ballbearing";
if (preg_match("/{$t}/i", $text)) {
if (preg_match("/{$p}/i", $text)) {
echo "Match detected";
}
}