PHP上的正则表达式工作不正常
问题是$productid始终为1,并且无论$productpath是什么都不会更改,例如,如果productpath是PHP上的正则表达式工作不正常,php,regex,Php,Regex,问题是$productid始终为1,并且无论$productpath是什么都不会更改,例如,如果productpath是/store/gst/prod_4,它仍然等于1请尝试: $productid = preg_match('/^.*?_/', $ProductPath); ShowProduct($productid); 如果您只想获取下划线前的前几个字符,则可以使用: (只有在以下情况下使用正则表达式才有意义:(1)正确使用;(2)还要验证前几个字符实际上是数字\d+)preg\u ma
/store/gst/prod_4
,它仍然等于1请尝试:
$productid = preg_match('/^.*?_/', $ProductPath);
ShowProduct($productid);
如果您只想获取下划线前的前几个字符,则可以使用:
(只有在以下情况下使用正则表达式才有意义:(1)正确使用;(2)还要验证前几个字符实际上是数字
\d+
)preg\u match返回匹配数。这意味着你的模式只匹配一次。如果想要得到结果,需要使用preg_match的第三个参数
也许这会有帮助
$productid = preg_match('#(prod_)([0-9]+)#', $ProductPath);
ShowProduct($productid[1]);
请阅读手册中的preg_match。
$productid = strtok($ProductPath, "_");
$productid = preg_match('/^.*?_/', $ProductPath, $match);
print_r($match);
$productid = preg_match('#(prod_)([0-9]+)#', $ProductPath);
ShowProduct($productid[1]);
preg_match( '/^.*?_(\d+)/', $ProductPath, $matches );
$productid = $matches[1];