这个正则表达式实现了什么


What does this regex achieve?

我见过这个...

preg_match("/.*" . $row['keyword'] . ".*/", $word, $matches);

模式试图暗示什么?

表示查找后面或前面有 0 个或多个字符 ( * ) 的术语,这些字符不是'n. )。

除非在其他地方完成,否则您应该用preg_quote($row['keyword'], '/')包裹$row['keyword']

解释:

/                            # start of the regex
.                            # match anything (any character, etc - except for /n)
*                            # zero or more times
" . $row['keyword']. "       # match the keyword
.*                           # same as above
/                            # end of the regex

/只是模式分隔符,.*表示"任何字符(换行符除外)重复 0 次或更多次",因此它只是搜索$row['keyword']$word 中出现的任何字符串。

它试图找出$row['keyword']是否包含在$word中,如果关键字包含元字符(如*/'等,调用preg_quote$row['keyword']也更安全