正则替换html中的src路径为全路径
正则替换html中的src路径为全路径
使用正则表达式替换内容
$content = '<p><img src="/uploads/image/20200818/1597720891186314.png" title="1597720891186314.png" alt="score1.png"/></p><p><video class="edui-upload-video vjs-default-skin video-js" controls="" preload="none" width="420" height="280" src="/uploads/video/20200818/1597720911477773.mp4" data-setup="{}"></video></p>';print_r($content);echo '<br/>';echo "======================之前===================";echo '<br/>'; $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; }}
结果
======================之前===================<p><img src="/uploads/image/20200818/1597720891186314.png"title="1597720891186314.png" alt="score1.png"/></p> <p> <video class="edui-upload-video vjs-default-skin video-js" controls="" preload="none" width="420" height="280" src="/uploads/video/20200818/1597720911477773.mp4" data-setup="{}"></video></p> ======================现在=================== <p><img src="http://sxkj.cn/uploads/image/20200818/1597720891186314.png"title="1597720891186314.png" alt="score1.png"/></p> <p> <video class="edui-upload-video vjs-default-skin video-js" controls="" preload="none" width="420" height="280" src="http://sxkj.cn/uploads/video/20200818/1597720911477773.mp4" data-setup="{}"></video> </p>
赞 (0)