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
猜你需要

豫ICP备2021026617号-1  豫公网安备:41172602000185   Copyright © 2021-2028 www.78moban.com/ All Rights Reserved

本站作品均来自互联网,转载目的在于传递更多信息,并不代表本站赞同其观点和对其真实性负责。如有侵犯您的版权,请联系 1565229909#qq.com(把#改成@),我们将立即处理。