[英]Preg_Match a string in the form of a URL
我有一個URL作為字符串。 如何在VideoID之后匹配數字。 VideoID也可能出現在URL中的不同位置。 但是我以后會擔心,因為我什至無法做到這一點。
$string = 'http://example.com/index.php?action=vids.individual&VideoID=60085484';
preg_match('/(?<VideoID>)=/', $string, $matches);
print_r($matches);
...為菜鳥保留一些零錢。 :)
/(?:\?|&)VideoID=([0-9]+)/ # get just the ID, stored in \\1
/(?:\?|&)(VideoID=[0-9]+)/ # get VideoId=ID, stored in \\1
在假定您的URL格式正確的前提下,始終以?
開頭?
或&
,並且在您的示例中,URL是嚴格數字的,因此它將與有效ID匹配,直到URL的下一段。
$string = 'http://example.com/index.php?action=vids.individual&VideoID=60085484&somethingelse';
$s = explode("VideoID=",$string);
print preg_replace("/[^0-9].*/","",$s[1]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.