php img函数保存,php img 标签 src 属性添加域名函数

/**

* 替换编辑器中的图片

* @param  string $content 要替换的内容

* @param  string $strUrl 内容中图片要加的域名

* @param  integer $type 类型:0=减少域名,1=增加域名

* @return string

* @eg

*/

function replacePicUrl($content = null, $strUrl = null, $type=0)

{

if ($strUrl) {

//提取图片路径的src的正则表达式 并把结果存入$matches中

        preg_match_all("/]+>/isU",$content,$matches);

$img = "";

if(!empty($matches)) {

//注意,上面的正则表达式说明src的值是放在数组的第三个中

$img = $matches[2];

}else {

$img = "";

}

if (!empty($img)) {

$patterns= array();

$replacements = array();

foreach($img as $imgItem){

if($type){

$final_imgUrl = $strUrl.$imgItem;

}else{

$final_imgUrl = str_replace($strUrl, '', $imgItem);

}

$replacements[] = $final_imgUrl;

$img_new = "/".preg_replace("/\//i","\/",$imgItem)."/";

$patterns[] = $img_new;

}

//让数组按照key来排序

ksort($patterns);

ksort($replacements);

//替换内容

$vote_content = preg_replace($patterns, $replacements, $content);

return $vote_content;

}else {

return $content;

}

} else {

return $content;

}

}

使用方法

//处理编辑器中图片

$detail = '

111

';

$detail = replacePicUrl($detail, 'http://www.baidu.com', 1);

var_dump($detail);

处理结果

string(363) "

111

"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值