简体   繁体   中英

PHP - Get a String from String using preg_match in PHP

I have many similar URLs. And i want to get a string from them.

For example the URL is:- http://www.amazon.in/Sinew-Nutrition-Decaffeinated-Unroasted-Management/dp/B01LF7Y0S4/ref=sr_1_1?ie=UTF8&qid=1484716249&sr=8-1&keywords=green+coffee+beans

And I want to get "B01LF7Y0S4" value (without quotes)

We can consider "/dp/"the start point and the very first "/" after "/dp/" the end point.

Thanks in advance.

You can try this:

\/dp\/([^\/]*)\/

Explanation

Sample Code:

$re = '/\/dp\/([^\/]*)\//';
$str = 'http://www.amazon.in/Sinew-Nutrition-Decaffeinated-Unroasted-Management/dp/B01LF7Y0S4/ref=sr_1_1?ie=UTF8&qid=1484716249&sr=8-1&keywords=green+coffee+beans';
preg_match_all($re, $str, $matches);
echo $matches[1][0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM