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.

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

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集