PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27229113/article/details/82888360
/**
* 图片地址替换成压缩URL
* @param string $content 内容
* @param string $suffix 后缀
*/
function get_img_thumb_url($content="",$suffix="!c550x260.jpg")
{
// by http://www.manongjc.com/article/1319.html
$pregRule = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp]))[\'|\"].*?[\/]?>/";
$content = preg_replace($pregRule, '<img src="${1}'.$suffix.'" style="max-width:100%">', $content);
return $content;
}
//by http://www.manongjc.com
$content = '<a href="#"><img class="center" src="https://xxx.com/styles/images/default.jpg"></a>'
.'<p><img class="center" src="https://img.xxx.com/images/219_Ig5eZI.jpg" style="max-width: 100%;"></p>';
$newct = get_img_thumb_url($content);
print_r($newct);
<a href="#"><img src="https://xxx.com/styles/images/default.jpg!c550x260.jpg" style="max-width:100%"></a><p><img src="https://img.xxx.com/images/219_Ig5eZI.jpg!c550x260.jpg" style="max-width:100%"></p>

自己的

 /**
     * 图片地址替换成压缩URL
     * @param string $content 内容
     * @param string $suffix 后缀
     */
    function get_img_thumb_url($content="",$suffix="")
    {
        $pregRule = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp]))[\'|\"].*?[\/]?>/";
        $content = preg_replace($pregRule, '<img src="'.$suffix.'${1}" style="max-width:100%">', $content);
        return $content;
    }

调用

 $params['maincontent'] = $this->get_img_thumb_url($params['maincontent'],'https://sali.vr68.com');

结果

<div style="text-align: center;"><b><span style="font-size: 18px;">中国杯帆船赛</span></b></div><div style="text-align: center;"><div style="text-align: left;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; text-align: start; text-indent: 28px;">中国杯帆船赛是中国历史上第一个国际性大</span><a target="_blank" href="https://baike.baidu.com/item/%E5%B8%86%E8%88%B9" style="color: rgb(19, 110, 194); font-family: arial, 宋体, sans-serif; font-size: 14px; text-align: start; text-indent: 28px; background-color: rgb(255, 255, 255);">帆船</a><span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; text-align: start; text-indent: 28px;">赛事,亦是唯一以“中国杯”命名的大帆船赛事,也是亚太地区最大规模的Beneteau First 40.7统一设计组别大帆船赛事。 曾经于2008年、2009年连续两度荣膺“亚太地区最佳帆船赛事”,2010年-2011年,中国杯帆船赛被正式列入ISAF(国际帆联)大帆船赛事的日历,是国内第一个进入国际帆联历的大帆船赛事。2015年,国际帆船联合会授予“推动航海运动特别奖”。</span></div><div style="text-align: left;"><img src="https://sali.vr68.com/uploads/20180928/78c11aa134e99066a726c0ca408d4608.png" style="max-width:100%"><span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; text-align: start; text-indent: 28px;"><br></span></div><div style="text-align: left;"><img src="https://sali.vr68.comhttps://sali.vr68.com/uploads/20180928/fbf8799a777b1a09e05a5e16b65c5489.png" style="max-width:100%"><span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; text-align: start; text-indent: 28px;"><br></span></div><span style="font-size: 18px;"></span></div>

猜你喜欢

转载自blog.csdn.net/qq_27229113/article/details/82888360