当前位置:首页 > 技术分享

php js正则 解决 详情页图片超出问题 匹配img标签内容追加style样式,给富文本中的图片设置大小和居中显示

admin3年前 (2023-03-15)技术分享2136

做小程序或手机端经常会遇到商品详情,文章详情里带图片的从别的地方复制过来带着样式,导致超出屏幕显示不全的问题


php解决

function format_img($content,$style=''){
    if(empty($style))$style = 'max-width:100%;height:auto;';
    $content= preg_replace("/(\<img)(?!(.*?style=['\"](.*)['\"])[^\>]+\>)/i", "$1 style=\"\"$2" , $content); 
    return preg_replace("/(\<img.*?style=['\"])([^\>]+\>)/i", "$1".$style."$2" , $content); 
}
//如果上面无法替换可以小改一下,用下面这个
function format_img($content,$style=''){
    if(empty($style))$style = 'max-width:100%;height:auto;';
    $content= preg_replace("/(\<img)(?!(style=['\"](.*)['\"])[^\>]+\>)/i", "$1 style=\"\"$2" , $content); 
    return preg_replace("/(\<img.*?style=['\"])([^\>]+\>)/i", "$1".$style."$2" , $content); 
}

Js解决

function format_img(htmlstr){
	//正则匹配不含style="" 或 style='' 的img标签
	var regex1 = new RegExp("(i?)(\<img)(?!(.*?style=['\"](.*)['\"])[^\>]+\>)","gmi");
	//给不含style="" 或 style='' 的img标签加上style=""
	htmlstr = htmlstr.replace(regex1, "$2 style=\"\"$3");
        //正则匹配含有style的img标签
	var regex2 = new RegExp("(i?)(\<img.*?style=['\"])([^\>]+\>)","gmi");
	htmlstr = htmlstr.replace(regex2, "$2max-width:100%;height:auto;border:5px solid red;$3");
	return htmlstr;
}


还有一种比较变态的做法是直接用正则表达式删除img标签的width、height、style

<?php
$txt1='<a href="https://blog.bitefu.net/post/501.html" style="margin:0 auto;border-radius:6px; background-color: #ffb808;display: block;text-align: center;padding: 12px 12px 12px 12px;color:#fff; width: 180px;font-weight: bold;">查看历史中奖记录</a><img src="http://www.baidu.com/Uploads/ueditor/image/20170512/1494556977462196.jpg" title="1494556977462196.jpg" alt="timg.jpg" style="width: 524px; height: 689px;" width="524" height="689"><img src="http://www.baidu.com/Uploads/ueditor/image/20170512/1494556977462196.jpg" title="1494556977462196.jpg" alt="timg.jpg" style="width: 524px; height: 689px;" width="524" height="689">';
$txt1 = preg_replace( '/(<img.*?)(style=.+?[\'|"])|((width)=[\'"]+[0-9]+[\'"]+)|((height)=[\'"]+[0-9]+[\'"]+)/i', '$1' , $txt1); 
echo $txt1;
?>


扫描二维码推送至手机访问。

版权声明:本文由小刚刚技术博客发布,如需转载请注明出处。

本文链接:https://blog.bitefu.net/post/501.html

分享给朋友:

“php js正则 解决 详情页图片超出问题 匹配img标签内容追加style样式,给富文本中的图片设置大小和居中显示” 的相关文章

百度云,天翼云盘解析网页地址 收集分享

百度云解析http://p.106666.xyz/https://pan.kdbaidu.com/https://pan.kdpd.me/https://yun.kdbaidu.com/http://blog.xxatf.top/https…

centos 配置Let's Encrypt 泛域名https证书

centos 配置Let's Encrypt 泛域名https证书

前言2018年1月份Letsencrypt可以申请泛域名证书,这让我们部署多域名、多站点https省了很多功夫,终于可以不用维护多个域名的https证书。笔者以acme.sh为例,手把手教你配置https证书~本教程适用于centos 6.…

微软Windows 10升级密钥(例如家庭版升级为企业版) 不能用于激活系统

微软Windows 10升级密钥(例如家庭版升级为企业版) 不能用于激活系统

下面的密钥,是微软官方提供的,仅能用于Windows10系统版本的升级,比如从家庭版升级为专业版、专业版升级为企业版等。升级密钥不能用于激活系统,激活需要KMS或者数字权利,由于涉及到版权问题,在此不宜分享,请大家自行查找激活相关的内容。准…

抢先体验太阳谷!20H1、20H2、21H1替换“Dev版新图标”

尽量不要替换shell32.dll.mun和zipfldr.dll.mun,这两老哥可能会带来无法预测的风险!不解除被替换文件的硬链接,已确定会导致无法安装质量更新!(详见H大测评)单替换imageres.dll.mun新图标覆盖率基本可达…

Chrome 最全历史版本下载以及chromedriver下载

https://vikyd.github.io/download-chromium-history-version/#/ https://dl.lancdn.com/landian/soft/chrome/m/ chromedriver的版…

记一次阿里云服务器cc攻击防护 windows 2012 iis8

记一次阿里云服务器cc攻击防护 windows 2012 iis8

上次连续一周左右阿里云服务器都在遭受cc攻击.导致访问量特别大,节假日接口调用特别缓慢或者根本访问不了的情况.本身服务器安装了 网站安全狗(IIS版) .并开始了防cc攻击.但是呢,平时还行,这次压力山大.于是一气之下用pytho…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。