簡體   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