rusly
rusly

Reputation: 1522

REGEX: get specific number in url

i try to get product id from this url, the pattern is try to find number after p- and before .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

result should be:

362060
665322

my current code:

$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;

Upvotes: 0

Views: 128

Answers (2)

Andrew
Andrew

Reputation: 5340

What you want is preg_match, not preg_replace.

preg_match ( '/(\d+)\.html/', $subject, $matches );

echo $matches [1];

Upvotes: 1

Keeper
Keeper

Reputation: 3566

You should use parenthesis to match exacly like this:

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

For example the first string matches like this:

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

Upvotes: 4

Related Questions