使用PHP正则表达式获取'live21.hbtv.com.cn_hbtv-hbws-'后的数字的方法如下:

$text = '#EXTINF:5.000,  
live21.hbtv.com.cn_hbtv-hbws-1695970815817.ts?auth_key=1695971147-ea686b005e9511ee93015958feefb538-0-ac5d4284c30e943d5bb7375776f22948  
#EXTINF:5.000,  
431/live21.hbtv.com.cn_hbtv-hbws-1695970820836.ts?auth_key=1695971147-ea686b005e9511ee93015958feefb538-0-ffff7fc00c810489ba75a86df7ef3873  
#EXTINF:5.000,  
431/live21.hbtv.com.cn_hbtv-hbws-1695970825796.ts?auth_key=1695971147-ea686b005e9511ee93015958feefb538-0-5d27fbeb7f374651a7951cba7e3b9847  
#EXTINF:5.000,  
live21.hbtv.com.cn_hbtv-hbws-1695970830793.ts?auth_key=1695971147-ea686b005e9511ee93015958feefb538-0-7760b3ed7709386ae4d4f0a9ffe479f9  
#EXTINF:5.000,  
live21.hbtv.com.cn_hbtv-hbws-1695970835826.ts?auth_key=1695971147-ea686b005e9511ee93015958feefb538-0-3c542320db4ece6452abe94f7e4f3478  
#EXTINF:5.000,  
live21.hbtv.com.cn_hbtv-hbws-1695970840924.ts?auth_key=1695971147-ea686b005e9511ee93015958feefb538-0-72e732ed941422e5f1ba689b0c299c43';

preg_match_all('/live21\.hbtv\.com\.cn_hbtv-hbws-([\d]+)\.ts/', $text, $matches);

$result = $matches[1];
print_r($result);

输出结果:

Array
(
    [0] => 1695970815817
    [1] => 1695970820836
    [2] => 1695970825796
    [3] => 1695970830793
    [4] => 1695970835826
    [5] => 1695970840924
)

其中 $result 数组包含了匹配的结果,即 “live21.hbtv.com.cn_hbtv-hbws-” 后的数字部分。

代码解释:

  • preg_match_all 函数用于执行正则表达式匹配。
  • '/live21\.hbtv\.com\.cn_hbtv-hbws-([\d]+)\.ts/' 是正则表达式,用于匹配目标字符串。
    • live21\.hbtv\.com\.cn_hbtv-hbws- 匹配目标字符串中的 “live21.hbtv.com.cn_hbtv-hbws-” 部分。
    • ([\d]+) 匹配一个或多个数字,并将其捕获到分组中。
    • \.ts 匹配“.ts” 后缀。
  • $matches 是一个数组,包含匹配结果。
  • $matches[1] 数组包含所有捕获到的数字。

使用正则表达式可以方便地从字符串中提取特定信息,提高代码效率和可读性。

使用PHP正则表达式获取“live21.hbtv.com.cn_hbtv-hbws-”后的数字

原文地址: https://www.cveoy.top/t/topic/q6z 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录