簡體   English   中英

Preg_Match一個URL形式的字符串

[英]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);

...為菜鳥保留一些零錢。 :)

只需使用內置的parse_url / parse_str

$string = 'http://example.com/index.php?action=vids.individual&VideoID=60085484';
$URL = parse_url($string);
parse_str($URL['query'],$Q);
print_r($Q);

回報

 Array ( [action] => vids.individual [VideoID] => 60085484 ) 
/(?:\?|&)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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM