2017-10-20 63 views
-1

我需要preg_match指向imgur.com服務的鏈接。 因此,我的代碼是:Preg matching imgur.com鏈接

if (!preg_match("/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3} 
(\/\$_POST['request'])) { ... } 

如何使它只與這些鏈接一起工作?

http://imgur.com/aBc1 
    https://imgur.com/a/aBc1 
http://www.imgur.com/a/aBc1 
    https://www.imgur.com/a/aBc1 
http://imgur.com/gallery/aBc1 
    https://imgur.com/gallery/aBc1 
http://i.imgur.com/aBc1.png(.jpg,.jpeg,.gif) 
    https://i.imgur.com/aBc1.png(.jpg,.jpeg,.gif) 
+0

你關心的所有捕獲組?意思是說,你想訪問URL中的任何值嗎?或者你只需​​要知道它是或不是一個imgur網址? –

+0

@PatrickQ,最後一個。 –

回答

1

這應該這樣做:

/(http|https)\:\/\/[a-z]{0,4}[\.]*imgur+\.[a-zA-Z]{2,3}\/[a-zA-Z0-9]+(\/)*[a-zA-Z0-9]*[\.]*(jpg|png|jpeg|gif)*/g 
  • (http|https):在URL檢查httphttps
  • [a-z\.]{0,4}:檢查www./i.
  • (jpg|png|jpeg|gif)*檢測到您給出的格式(如果有的話)

現場演示here

+0

不工作,最後是「g」還是需要的? –

+0

好吧,刪除「g」,工作。 –

1

此模式將匹配所有你提供

/http(s)?\:\/\/(.*\.)?imgur.com\/.*/ 

解釋URL的:

  • http匹配文字字符 'HTTP'
  • (s)?可選最多匹配'
  • \:\/\/匹配文字字符「://」
  • (.*\.)?任選地匹配的任意字符後跟一個句點/點的任何量
  • imgur.com\/匹配「imgur.com/」
  • .*匹配任何數目的任何附加的字符

DEMO

+0

非常感謝,哥們 –