实现替换文章内图片的相对地址
在PC端和移动端不用同一个域名时,如果图片保存地址是相对路径时,显示时图片地址就会出错,下面是将文章内所有图片地址加上域名
$pregRule = "/<[img|IMG].*?src=[\'|\"](?!((http|https):\/\/www))(.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp]))[\'|\"].*?[\/]?>/"; $content = preg_replace($pregRule, '<img src="'.$cfg_weburl.'${3}" >', $row['content']); echo $content;
preg_match_all('/<img .*?src=[\"|\'](.+?)[\"|\'].*?>/', $row['content'], $strResult, PREG_PATTERN_ORDER); $content = $row['content']; foreach ($strResult[1] as $val) { if (strpos($val, 'www') === false) { $content = str_replace($val, $cfg_weburl . $val , $content); }