【问题标题】:PHP - Get a String from String using preg_match in PHPPHP - 在 PHP 中使用 preg_match 从字符串中获取字符串
【发布时间】:2017-01-31 11:50:58
【问题描述】:

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

例如网址是:-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/”之后的第一个“/”视为终点。

提前致谢。

【问题讨论】:

标签: php regex


【解决方案1】:

你可以试试这个:

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

Explanation

示例代码:

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

【讨论】:

  • 谢谢 Maverick_Mrt。这解决了我的问题。 :)
猜你喜欢
  • 1970-01-01
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-13
相关资源
最近更新 更多