REGEX:获取url中的特定数字


REGEX: get specific number in url

我尝试从这个url获取产品id,模式是尝试在p-之后和.html 之前查找数字

http://www.domain.com/jp-37025-shoes-red-p-362060.html?stores=203
http://www.domain.com/pp-66743-shoes-red-p-665322.html?stores=12

结果应该是:

362060
665322

我当前的代码:

$subject = "http://www.domain.com/jp-37025-shoes-red-p-362060.html?stores=203";
$patern = '/'W*[a-z]'D*/';
$string = preg_replace($patern, '', $subject);
echo $string;

您应该使用括号来匹配,如下所示:

preg_match("/p-('d+)'.html/", $input_line, $output_array);

例如,第一个字符串匹配如下:

Array
(
    [0] => p-362060.html
    [1] => 362060
)

您想要的是preg_match,而不是preg_replace。

preg_match ( '/('d+)'.html/', $subject, $matches );
echo $matches [1];