我有一个变量要在 preg_match 中使用,并结合一些正则表达式:
$string = "cheese-123-asdf";
$find = "cheese";
if(preg_match("/$find-/d.*/", $string)) {
echo "matched";
}
在我的模式中,我尝试使用奶酪进行匹配,然后是 - 和 1 位数字,然后是其他任何内容。
/d
为\d
.*
/
或*
或...
)),这可能会导致您的匹配出现问题。代码:
<?php
$string = "cheese-123-asdf";
$find = "cheese";
if(preg_match("/$find-\d/", $string))
{
echo "matched";
}
?>
/
您输入错误\
:
if(preg_match("/$find-\d.*/", $string)) {
这.*
也不是真正必要的,因为模式将匹配任何一种方式。
对于数字,它是\d
if(preg_match("/$find-\d.*/", $string)) {