繁体   English   中英

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

[英]PHP - Get a String from String using preg_match in PHP

我有很多类似的网址。 我想从他们那里得到一个字符串。

例如,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

而且我想获取“ B01LF7Y0S4”值(不带引号)

我们可以考虑以“ / dp /”为起点,并以“ / dp /”为终点的第一个“ /”。

提前致谢。

您可以尝试以下方法:

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

说明

样例代码:

$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