0

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

2 Answers 2

4

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
)
Sign up to request clarification or add additional context in comments.

1 Comment

it's ok i found the pattern something like this /_*[0-9]*(?:_(\d+))/
1

What you want is preg_match, not preg_replace.

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

echo $matches [1];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.