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

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

I have a URL which changes it position sometime

my urls look like

https://video.twimg.com/ext_tw_video/841791481534803968/pu/vid/320x180/egCAExYQdRBw08fb.mp4
https://video.twimg.com/ext_tw_video/841791481534803968/pu/vid/1280x720/GVsCGddpL9QoO4_B.mp4
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

$breaker=(explode("/",$video->url));
$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 23: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.

    preg_match("/\d+x\d+/", $input_line, $output_array);
    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.

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

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了