我想要一个php正则表达式来从这些中提取id(VmzI60RQG0fuw)
http://i.giphy.com/VmzI60RQG0fuw.gif
https://media.giphy.com/media/VmzI60RQG0fuw/giphy.gif
http://giphy.com/gifs/VmzI60RQG0fuw
http://giphy.com/gifs/music-videos-mariah-carey-dreamlover-VmzI60RQG0fuw
我测试了这个,但它在3概率上不起作用
preg_match('~(media'.giphy'.com/media/([^ /]+)/giphy'.gif|i.giphy.com/([^ /]+).gif)~i', $this->url, $matches)
这里有一个应该工作的:
'~https?://(?|media'.giphy'.com/media/([^ /]+)/giphy'.gif|i'.giphy'.com/([^ /]+)'.gif|giphy'.com/gifs/(?:.*-)?([^ /]+))~i'
查看regex演示
第三种选择是giphy'.com/gifs/(?:.*-)?([^ /]+)
:
giphy'.com/gifs/
-子路径giphy.com/gifs/
(?:.*-)?
-(可选组,由于(?:...)?
结构,文本可能会丢失)匹配最后一个-
之前的所有字符(也许,用'S*-
或[^'s/]*-
替换可以使其更精确)([^ /]+)
-匹配并捕获ID(除了空格和/
之外的一个或多个字符)
由于它是针对PHP的,您还可以使用分支重置((?|...|...)
),以便所有捕获的组都可以有一个相同的ID#1。