正则替换html中的src路径为全路径
使用正则表达式替换内容$content = '
title="1597720891186314.png" alt="score1.png"/>
';echo "======================之前===================";echo '
'; $t = new transFormation();$result = $t->transFormationContentImgSrc($content,'http://sxkj.cn');print_r($result);exit();/**
* 格式转换类
* Class transFormation
* author: yuxiangShi<18538187569@163.com>
* Date: 2021/5/6 10:30 下午
*/class transFormation{
/**
* 获得图片 返回全路径数组
* @param $content
* @param string $oriweb
* @return string|string[]
* author: yuxiangShi<18538187569@163.com>
* Date: 2021/5/6 10:30 下午
*/
function transFormationContentImgSrc($content,$oriweb='http://xxgc.cqipc.edu.cn/'){
//匹配图片的src
preg_match_all('#.*?src="([^"]*)"[^>]*>#i', $content, $match);
foreach($match[1] as $imgurl){
$imgurl = $imgurl;
if(is_int(strpos($imgurl, 'http'))){
$arcurl = $imgurl;
} else {
$arcurl = $oriweb.$imgurl;
}
$content=str_replace($imgurl,$arcurl,$content);
}
return $content;
}}
结果======================之前===================
src="/uploads/video/20200818/1597720911477773.mp4" data-setup="{}">
======================现在===================
height="280" src="http://sxkj.cn/uploads/video/20200818/1597720911477773.mp4" data-setup="{}">
本文介绍了一个PHP类`transFormation`,用于将HTML内容中的图片src相对路径转换为全路径。通过正则表达式匹配src属性,结合给定的基础URL,将每个相对路径替换为完整URL。示例中展示了如何使用该类处理一个包含视频src的HTML片段,将src转换为全路径。
1410

被折叠的 条评论
为什么被折叠?



