PHP正则表达式提取URL中的数字:解析/vodplay/328295-1-1.html
使用PHP正则匹配获取'/vodplay/328295-1-1.html'中的内容可以使用preg_match函数。正则表达式如下:
$pattern = '/\/vodplay\/(\d+-\d+-\d+)\.html/';
其中,\d+表示匹配1个或多个数字,-表示匹配一个横杠,\.表示匹配一个点(需要转义),最后的html表示匹配字符串结尾的html。
代码示例:
$txt = '<a href="/vodplay/328295-1-1.html" class="play-btn icon-btn"><a href="/voddetail/339927.html" class="play-btn-o"><span>详情</span></a>';
$pattern = '/\/vodplay\/(\d+-\d+-\d+)\.html/';
if(preg_match($pattern, $txt, $matches)){
echo $matches[0]; // 输出:/vodplay/328295-1-1.html
echo $matches[1]; // 输出:328295-1-1
}
注意,正则表达式中使用了括号()进行分组,匹配结果会存放在$matches数组中,$matches[0]表示整个匹配结果,$matches[1]表示第一个分组的匹配结果。
原文地址: http://www.cveoy.top/t/topic/oi06 著作权归作者所有。请勿转载和采集!