Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP上的正则表达式工作不正常_Php_Regex - Fatal编程技术网

PHP上的正则表达式工作不正常

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

问题是$productid始终为1,并且无论$productpath是什么都不会更改,例如,如果productpath是
/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];