首页 > 编程语言 >php 去除图片以及DIV的width、height、style

php 去除图片以及DIV的width、height、style

时间:2023-12-11 13:22:06浏览次数:37  
标签:style string preg height content width str 去除 replace

1.去掉图片的宽高,去掉DIV的style样式

$str = '<div style="margin: 0px auto; width: 740px;"> <p><img width="748" height="444" alt="" src="/images/upload/Image/manmiao_0001.jpg" /></p></div>';

//去掉图片宽度$search = '/(<img.*?)width=(["\'])?.*?(?(2)\2|\s)([^>]+>)/is';echo preg_replace($search,'$1$3',$str); $search1 = '/(<img.*?)height=(["\'])?.*?(?(2)\2|\s)([^>]+>)/is'; //去除图片的高度echo preg_replace($search1,'$1$3',$str);

 $str = '<div style="margin: 0px auto; width: 740px;"> <p><img width="748" height="444" alt="" src="1.jpg" /></p></div>';echo preg_replace("/style=.+?['|\"]/i",'',$str); //去除style样式

 

2.如何用php正则去掉得到img标签中的 属性

请问php正则如何去掉得到img标签中的 border属性,例如:<img src=”test.gif” border=”0″ alt=”test1″ />   替换后的  <img src=”test.gif” alt=”test1″ />

<?php//php过滤img标签中的 border属性$str = '<img src="test1.jpg" border="1" /><img src=\'test2.jpg\' border=\'2\' /><img src=test3.jpg border=3 />';$search = '/(<img.*?)border=(["\'])?.*?(?(2)\2|\s)([^>]+>)/is';echo preg_replace($search,'$1$3',$str);//highlight_string($r); //输出:/*<img src="test1.jpg" /><img src='test2.jpg' /><img src=test3.jpg />*/?>

 

3.PHP正则提取或替换img标记属性 

/*PHP正则提取图片img标记中的任意属性*/$str = '<center><img src="1.jpg" height="120" width="120"><br />PHP正则提取或更改图片img标记中的任意属性</center>'; //1、取整个图片代码preg_match('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i',$str,$match);echo $match[0];

//2、取widthpreg_match('/<img.+(width=\"?\d*\"?).+>/i',$str,$match);echo $match[1]; 

//3、取heightpreg_match('/<img.+(height=\"?\d*\"?).+>/i',$str,$match);echo $match[1]; //4、取srcpreg_match('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i',$str,$match);echo $match[1]; 

/*PHP正则替换图片img标记中的任意属性*///将src="/uploads/images/20100516000.jpg"替换为src="/uploads/uc/images/20100516000.jpg")print preg_replace('/(<img.+src=\"?.+)(images\/)(.+\.(jpg|gif|bmp|bnp|png)\"?.+>)/i',"\${1}uc/images/\${3}",$str);echo "<hr/>";

 //将src="/uploads/images/20100516000.jpg"替换为src="/uploads/uc/images/20100516000.jpg",并省去宽和高print preg_replace('/(<img).+(src=\"?.+)images\/(.+\.(jpg|gif|bmp|bnp|png)\"?).+>/i',"\${1} \${2}uc/images/\${3}>",$str);

 htmlentities 将所有的字元都转成 HTML 字串 语法: string htmlentities(string string); 传回值: 字串 函式种类: 资料处理 内容说明 解析:本函式有点像 htmlspecialchars() 函式,但本函式会将所有 string 的字元都转成 HTML 的特殊字集字串。不过在转换后阅读网页原始码的方面,会有很多困扰,尤其是网页原始码的中文字会变得不知所云,浏览器上看到的还是正常的。php 去除html标签 js 和 css样式 - 最爱用的一个PHP清楚html格式函数Function ClearHtml($content) { $content = preg_replace("/<a[^>]*>/i", "", $content); $content = preg_replace("/<\/a>/i", "", $content); $content = preg_replace("/<div[^>]*>/i", "", $content); $content = preg_replace("/<\/div>/i", "", $content); $content = preg_replace("/<!--[^>]*-->/i", "", $content);//注释内容 $content = preg_replace("/style=.+?['|\"]/i",'',$content);//去除样式 $content = preg_replace("/class=.+?['|\"]/i",'',$content);//去除样式 $content = preg_replace("/id=.+?['|\"]/i",'',$content);//去除样式 $content = preg_replace("/lang=.+?['|\"]/i",'',$content);//去除样式 $content = preg_replace("/width=.+?['|\"]/i",'',$content);//去除样式 $content = preg_replace("/height=.+?['|\"]/i",'',$content);//去除样式 $content = preg_replace("/border=.+?['|\"]/i",'',$content);//去除样式 $content = preg_replace("/face=.+?['|\"]/i",'',$content);//去除样式 $content = preg_replace("/face=.+?['|\"]/",'',$content);//去除样式只允许小写正则匹配没有带 i 参数 return $content;}

 

strip_tags($str) 去掉 HTML 及 PHP 的标记语法: string strip_tags(string str); 传回值: 字串 函式种类: 资料处理 内容说明 :解析:本函式可去掉字串中包含的任何 HTML 及 PHP 的标记字串。若是字串的 HTML 及 PHP 标签原来就有错,例如少了大于的符号,则也会传回错误。而本函式和 fgetss() 有着相同的功能 PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。 PHP清除html、css、js格式并去除空格的PHP函数 function cutstr_html($string, $sublen) { $string = strip_tags($string); $string = preg_replace ('/\n/is', '', $string); $string = preg_replace ('/ | /is', '', $string); $string = preg_replace ('/&nbsp;/is', '', $string); preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $t_string); if(count($t_string[0]) - 0 > $sublen) $string = join('', array_slice($t_string[0], 0, $sublen))."…"; else $string = join('', array_slice($t_string[0], 0, $sublen)); return $string; }解析:这个函数既有去除html标签、css样式、js、空格等格式的功能(格式化html文本)也有截取字符串的功能。

/** * 过滤HTML代码空格,回车换行符的函数 * @param $str * @return mixed|string */ public static function deleteHtml($str){ $str = trim($str); $str = strip_tags($str,""); $str = strtr($str, array(' '=>'')); $str = strtr($str, array(' '=>'')); $str = str_replace(array("\r\n", "\r", "\n","\t","&nbsp;"," "), "", $str); return $str; }

标签:style,string,preg,height,content,width,str,去除,replace
From: https://www.cnblogs.com/akweb/p/17894168.html

相关文章

  • 如何不是用 width 和 height 控制 svg 的渲染大小?
    除了使用width和height属性控制SVG的渲染大小外,还可以使用以下方法:使用CSS样式:可以通过设置CSS的width和height属性来控制SVG的渲染大小。例如,设置SVG元素的样式为width:100px;height:100px;。使用viewBox属性:viewBox属性定义了SVG的可视区域,在该区域内进行缩放和裁剪。可以通过......
  • StyleSync 开源部分总结
    https://github.com/guanjz20/StyleSync_PyTorch这个是号称最强的模型.说百分之99拟合真人.我们赶紧来学习.首先权重和训练是不开源的.我也只能尽可能的根据发布的代码来看能学到什么.先说结论:整体跟wav2lip百分之90相似.都是视频--->图片--->抽取人脸landmark->每个图片......
  • el-table-column width="180" 宽度自动成比例缩放缩小 表头宽度不对 原因
    首先el-table-columnwidth="180"的设置原理是如下面加粗部分 <tablecellspacing="0"cellpadding="0"border="0"class="el-table__header"style="width:1638px;"><colgroup><colname="el-table_......
  • 前端组件wolfTable的style格式文档
    此文档记录的是wolf-table的style格式文档,如果你找的是x-data-spreadsheet,那么请查阅这个文档https://www.cnblogs.com/WilsonZhu/p/17858411.htmlwolf-table的格式输入只有一个方式,通过调用addStyle以及cell函数来进行格式的改变例子如下:importTablefrom'@wolf-table......
  • Qt中QStyledItemDelegate的使用(二)
    延续上一篇文章。本文给出了一个QStyledItemDelegate类自定义绘制加自定义编辑框的例子。为方便读者理清思路,我已经尽量简化本文附加的代码了。此程序模拟用户给出星级评价的效果,在编辑状态下用户可以设置0~5个星星的评价,在普通状态下界面显示对应数量的金黄色星星。本文代码在VS2......
  • Unity builtin GUIStyle内置样式
    https://gist.github.com/bikrone/666bb26fb0d4468df12c890ecc6c512eusingUnityEditor;usingUnityEngine;publicsealedclassExampleClass:EditorWindow{privatestaticreadonlystring[]mList={"AboutWIndowLicenseLabel"......
  • uniapp-微信小程序绑定动态样式 :style 避坑
    在uniapp中绑定动态样式:style="object"使用此种方法,在H5页面中并不会出现任何问题而在微信小程序中,此种方式就会被编译成 style="[object,object]"从而导致样式无法生效解决方法:    使用:style="[object]"此种方式即可......
  • CSS之min-height的使用
    1.关于min-height的使用效果2.不给height会造成的问题期望min-height生效3.直接给height会产生的问题期望被内容撑开的height生效注:no底边距,==>到底边的距离,懒得改了555,强迫症大免疫......
  • Can Report (rdlc) Table or Matrix Column Width Be Set at Runtime?
     UsinganrdlcreportinReportViewer,Ineedtocreateatableormatrixwherethenumberofcolumnsandthekindsofdatadisplayedinthecolumnschangeswitheachreport. Forexample,inonereport,thesecondcolumnmayholdpriceinformation. Ina......
  • maven 添加 checkstyle 插件约束代码规范
    本例示例,是引用http链接这种在线checkstyle.xml文件的配置方式,如下示例:<properties><maven.checkstyle.plugin.version>3.3.0</maven.checkstyle.plugin.version><!--支持本地绝对路径、本地相对路径、HTTP远程路径--><checkstyle.config.location>......