简体   繁体   English

PHP-使用PHP中的preg_match从字符串获取字符串

[英]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 例如,URL是:-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) 而且我想获取“ B01LF7Y0S4”值(不带引号)

We can consider "/dp/"the start point and the very first "/" after "/dp/" the end point. 我们可以考虑以“ / dp /”为起点,并以“ / dp /”为终点的第一个“ /”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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