js正则修改html中src的内容,正则替换html中的src路径为全路径

本文介绍了一个PHP类`transFormation`,用于将HTML内容中的图片src相对路径转换为全路径。通过正则表达式匹配src属性,结合给定的基础URL,将每个相对路径替换为完整URL。示例中展示了如何使用该类处理一个包含视频src的HTML片段,将src转换为全路径。
摘要由CSDN通过智能技术生成

正则替换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="{}">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值