Login
网站首页 > 文章中心 > php

PHP转换内容html串中的图片本地化并解析替换

作者:小编 更新时间:2023-07-04 08:48:31 浏览量:88人看过

public static function transImgLocal($html_contents,$a_baseSavePath,$a_localPathFlag){

$result=$html_contents;

    $html_contents = stripslashes($html_contents);//返回已剥离反斜杠的字符串

//echo $html_contents; exit;

$img_array = array();

//处理远程图片url

preg_match_all("/(src|SRC)=[\"|'| ]{0,}((http|https):\/\/(.*).(gif|jpg|jpeg|bmp|png))/isU", $html_contents, $img_array );

// 匹配出来的不重复图片

if(!empty($img_array)&& count($img_array)>=2){

$img_array = array_unique ( $img_array [2] );

//var_dump('img_array=',$img_array); exit;

//echo $img_array[0];

foreach($img_array as $item){

if(strpos($item,$a_localPathFlag) !== false){ continue; }

$filename=self::grabImage($item,$a_baseSavePath);

//echo $filename;

$html_contents=str_replace($item,'/'.$filename,$html_contents);

}

}

//处理base64图片格式

preg_match_all("/(src|SRC)=[\"|'| ]{0,}(data:image\/(\w+);base64,(.*))(\")/isU", $html_contents, $img_array2);

if(!empty($img_array2)){

  $img_array2 = array_unique ( $img_array2 [2] );

  //var_dump('img_array2=',$img_array2); exit;

foreach($img_array2 as $item){

if(strpos($item,$a_localPathFlag) !== false){ continue; }

$filename=self::base64_image($item,$a_baseSavePath);

//echo $filename; exit;

if(!empty($filename)){ $html_contents=str_replace($item,'/'.$filename,$html_contents); }

}

}

$result=$html_contents;

return $result;

  }


版权声明:倡导尊重与保护知识产权,本站有部分资源、图片来源于网络,如有侵权,请联系我们修改或者删除处理。
转载请说明来源于"土嘎嘎" 本文地址:http://www.tugaga.com/jishu/php/990.html
<<上一篇 2023-07-04
下一篇 >> 2023-07-04

编辑推荐

热门文章