Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy a specific part of string php

Tags:

php

web

How can I get a part of a string from the specific starting position to end. Like for example I want to copy B000ROAQ30 from the given url:

www.amazon.com/Black-Nokia-HS-47-Stereo-Headset/dp/B000ROAQ30/ref=pd_sim_hi_4

This B000ROAQ30 part can occour anywhere in the URL like in this case:

www.amazon.com/abc/cdfg/hij/dp/B000ROAQ30/ref=pd_sim_hi_4

How can I copy this B000ROAQ30?

Note B000ROAQ30 can be different but having B00 at the start.

Please ask questions if anything is not clear.

like image 619
Natasha Avatar asked Dec 30 '25 16:12

Natasha


1 Answers

This should work:

$url = 'www.amazon.com/Black-Nokia-HS-47-Stereo-Headset/dp/B000ROAQ30/ref=pd_sim_hi_4';
preg_match('/B00[0-9A-Z]+/', $url, $matches);

$result = $matches[0];

$result in this case will be: B000ROAQ30. Check it here: http://ideone.com/XrjIG

like image 61
Tadeck Avatar answered Jan 02 '26 07:01

Tadeck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!