详情页

zblog补全图片地址php版

时间:2023年09月25日

编辑:佚名

在使用api接口的时候,会发现,有些程序的图片报错规则是只存路径,这个时候就需要一个函数来补全内容里图片的完整路径了。
点击复制代码 PHP
function ytecn_imgallpath($str, $type=0) {
    $cfg_basehost='域名';
    $data ='';
    if(stripos($str, '<img') !== false){
        $arr= preg_split('/(<img.*?>)/i', $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
        foreach($arr as $k => $a){
            if(substr($a, 0, 4) == '<img') {
                $imgpath = array();
                preg_match('/<img.*?src="(.*?)"/i', $a, $imgpath);
                $imgpath = $imgpath[1];
                if(substr($imgpath, 0, 4) !== 'http' && substr($imgpath, 0, 4) !== 'https'){
                    $imgpath = $cfg_basehost.$imgpath;
                    $pattern = '/<img(.*?)src="(.*?)"(.*?)>/i';
                    $replacement = '<img${1}src="'.$imgpath.'"${3}>';
                    $a = preg_replace($pattern, $replacement, $a);
                }
            }
            $data .= $a;
        }
    }else{
        $data = $str;
    }
    return&nbsp
相关文章
猜你需要