dongxu198714 2018-06-11 15:21
浏览 26
已采纳

查找在URL中更改位置的字符串[duplicate]

I have a URL which changes it position sometime

my urls look like

  1. https://video.twimg.com/ext_tw_video/841791481534803968/pu/vid/320x180/egCAExYQdRBw08fb.mp4
  2. https://video.twimg.com/ext_tw_video/841791481534803968/pu/vid/1280x720/GVsCGddpL9QoO4_B.mp4
  3. https://video.twimg.com/ext_tw_video/841791481534803968/pu/vid/640x360/33qcUEzFw-WS0inJ.mp4

In these url i need to extract "320x180" , "1280x720" , "640x360" There are more and the list goes on.

Plus the link changes like sometimes there is no /pu/vid/ part in the url. I tried with Php explode function but as position changes. Its hard to find them. for example

https://video.twimg.com/ext_tw_video/640x360/pu/vid/841791481534803968/33qcUEzFw-WS0inJ.mp4

for the constant part i was using this code

  1. $breaker=(explode("/",$video->url));
  2. $quality = $breaker[6];

But since they change places and their values differ. Is there any different approach to it? Also if the values stay same then how can i extract the URL of the video quality

like if the script finds the quality is 1280x720 then it will give me the url.and will loop through them to find different static video qualities?

https://video.twimg.com/ext_tw_video/841791481534803968/pu/vid/1280x720/GVsCGddpL9QoO4_B.mp4
</div>
  • 写回答

1条回答 默认 最新

  • dongtui0650 2018-06-11 15:28
    关注

    You should just be able to use a preg_match() or preg_match_all() on the regex \d+x\d+, which matches an x preceded and followed by any number of digits.

    1. preg_match("/\d+x\d+/", $input_line, $output_array);
    2. preg_match_all("/\d+x\d+/", $input_lines, $output_array);

    This can be seen working on Regex101 here and PHPLiveRegex here.

    You could be a little more specific and use (?<=\/)\d{1,4}x\d{1,4}(?=\/), which ensures that there are between 1 and 4 digits in each group, and that the target string is enclosed by two /, though this is likely unnecessary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部