从网站a数据库中读取的新闻内容(html源码格式)写入网站b的新闻表中,格式不统一,而且有很多冗余代码,很多是从office复制过去的,需要过滤掉网站a新闻内容中冗余的html代码。新闻内容在php的$news字段中,给这个字段用正则表达式处理一下。
比如
<font id=888 style="font-size: 18px; font-family: font-size: 18px"><p><strong><span lang=en-us style="font-size: 16pt; font-family: 仿宋_gb2312; mso-hansi-font-family: calibri; mso-bidi-font-family: 'times new roman'; mso-ansi-language: en-us; mso-fareast-language: zh-cn; mso-bidi-language: ar-sa"> 一级</span><span style="font-size: 16pt; font-family: 仿宋_gb2312; mso-hansi-font-family: calibri; mso-bidi-font-family: 'times new roman'; mso-ansi-language: en-us; mso-fareast-language: zh-cn; mso-bidi-language: ar-sa">标题<span lang=en-us>粗体1</span></span></strong></p> <p><span style="font-size: 16pt; font-family: 仿宋_gb2312; mso-hansi-font-family: calibri; mso-bidi-font-family: 'times new roman'; mso-ansi-language: en-us; mso-fareast-language: zh-cn; mso-bidi-language: ar-sa">新闻内容</span></p> <p><span style="font-size: 16pt; font-family: 仿宋_gb2312; mso-hansi-font-family: calibri; mso-bidi-font-family: 'times new roman'; mso-ansi-language: en-us; mso-fareast-language: zh-cn; mso-bidi-language: ar-sa"><strong></strong></span></p> <p><span style="font-size: 16pt; font-family: 仿宋_gb2312; mso-hansi-font-family: calibri; mso-bidi-font-family: 'times new roman'; mso-ansi-language: en-us; mso-fareast-language: zh-cn; mso-bidi-language: ar-sa"><strong><img src="http://192.168.1.1/webimage/2222.jpg" style="max-width:90%"求关于正则表达式PHP过滤编辑器的新闻内容" ></strong></span></p> <p><span style="font-size: 16pt; font-family: 仿宋_gb2312; mso-hansi-font-family: calibri; mso-bidi-font-family: 'times new roman'; mso-ansi-language: en-us; mso-fareast-language: zh-cn; mso-bidi-language: ar-sa"><strong>一级标题粗体2</strong></span></p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p> <br><br><p><span style="font-size: 16pt; font-family: 仿宋_gb2312; mso-hansi-font-family: calibri; mso-bidi-font-family: 'times new roman'; mso-ansi-language: en-us; mso-fareast-language: zh-cn; mso-bidi-language: ar-sa"></span> </p> <p><span style="font-size: 16pt; font-family: 仿宋_gb2312; mso-hansi-font-family: calibri; mso-bidi-font-family: 'times new roman'; mso-ansi-language: en-us; mso-fareast-language: zh-cn; mso-bidi-language: ar-sa"><strong><img src="http://192.168.1.1/webimage/2233.jpg" style="max-width:90%"求关于正则表达式PHP过滤编辑器的新闻内容" ></strong></span></p> <p><span style="font-size: 16pt; font-family: 仿宋_gb2312; mso-hansi-font-family: calibri; mso-bidi-font-family: 'times new roman'; mso-ansi-language: en-us; mso-fareast-language: zh-cn; mso-bidi-language: ar-sa"></span></p> <p align=center><span style="font-family: 仿宋_gb2312; font-size: 16pt; mso-hansi-font-family: calibri; mso-bidi-font-family: 'times new roman'; mso-ansi-language: en-us; mso-fareast-language: zh-cn; mso-bidi-language: ar-sa">这段文字为居中</span></p><br><p align=right><span style="font-family: 仿宋_gb2312; font-size: 16pt; mso-hansi-font-family: calibri; mso-bidi-font-family: 'times new roman'; mso-ansi-language: en-us; mso-fareast-language: zh-cn; mso-bidi-language: ar-sa">这段文字为右对齐</span></p><br><p align=left><span style="font-family: 仿宋_gb2312; font-size: 16pt; mso-hansi-font-family: calibri; mso-bidi-font-family: 'times new roman'; mso-ansi-language: en-us; mso-fareast-language: zh-cn; mso-bidi-language: ar-sa">这段文字是斜体</span></p><br><p align=left><span style="font-family: 仿宋_gb2312; font-size: 16pt; mso-hansi-font-family: calibri; mso-bidi-font-family: 'times new roman'; mso-ansi-language: en-us; mso-fareast-language: zh-cn; mso-bidi-language: ar-sa"><a title="" href="http://www.baidu.com/">加一个链接</a></span></p></font>
要处理成
<p><strong>一级标题粗体</strong></p><p>新闻内容</p><p><img src="http://192.168.1.1/Webimage/2222.jpg" / alt="求关于正则表达式PHP过滤编辑器的新闻内容" ></p><p>第二段新闻正文</p><p><strong>一级标题粗体2</strong></p><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/ai/2663" title="靠岸学术"><img
src="https://img.php.cn/upload/ai_manual/001/503/042/69b3bf2c510bf638.jpeg" alt="靠岸学术" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/ai/2663" title="靠岸学术">靠岸学术</a>
<p>一款集翻译,阅读,文献管理于一体的英文文献阅读器</p>
</div>
<a href="/ai/2663" title="靠岸学术" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</div><p><img src="http://192.168.1.1/Webimage/2233.jpg" / alt="求关于正则表达式PHP过滤编辑器的新闻内容" ></p><p style="max-width:90%">这段文字为居中</p><p style="text-align: right;">这段文字为右对齐</p><p><em>这段文字为斜体</em></p><p><a href="http://www.baidu.com">加一个链接</a></p> 具体的代码说明写了个网页,方便大神看: http://www.sunmuu.com/help/editorHelp.html
后面是php连接查询的代码,方便测试,数据库mysql,表是editor,两个字段ID(INIT)和news(MEDIUMTEXT):
$mysql_db_hostname = "localhost";$mysql_db_user = "root";$mysql_db_password = "root";$mysql_db_database = "test";$con = mysqli_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password, $mysql_db_database);mysqli_query($con, "SET NAMES utf8");$sql="SELECT * FROM editor";$re=mysqli_query($con,$sql)or die("读取数据出错". mysqli_error());while($row=mysqli_fetch_array($re)){$str=$row["news"];echo $str;}
回复讨论(解决方案)
//去掉不允许的 tag$s = strip_tags($s,'<p>,<a>,<img alt="求关于正则表达式PHP过滤编辑器的新闻内容" >,<strong>,<em>');//去掉tag 里面的属性$s = preg_replace('/<(p|strong|em)[^>]+?>/i','<$1>',$s);//img 和 a 单独处理$s = preg_replace('/<(a|img).+?(href|src)="([^"]+?)"[^>]*?>/i','<$1 $2="$3">',$s);echo $s; 试试吧,其它不影响显示的你就自己去改吧
//去掉不允许的 tag$s = strip_tags($s,'<p>,<a>,<img alt="求关于正则表达式PHP过滤编辑器的新闻内容" >,<strong>,<em>');//去掉tag 里面的属性$s = preg_replace('/<(p|strong|em)[^>]+?>/i','<$1>',$s);//img 和 a 单独处理$s = preg_replace('/<(a|img).+?(href|src)="([^"]+?)"[^>]*?>/i','<$1 $2="$3">',$s);echo $s; 试试吧,其它不影响显示的你就自己去改吧
太感谢你了jam00!!
有一点,p标签里面的style align属性需要保留, style="max-width:90%"和style="text-align: left;",这个逻辑顺序是怎样的?可以给个提示吗?
//去掉不允许的 tag$s = strip_tags($s,'<p>,<a>,<img alt="求关于正则表达式PHP过滤编辑器的新闻内容" >,<strong>,<em>');//去掉tag 里面的属性$s = preg_replace('/<(strong|em)[^>]+?>/i','<$1>',$s);//img 和 a 单独处理$s = preg_replace('/<(a|img).+?(href|src)="([^"]+?)"[^>]*?>/i','<$1 $2="$3">',$s);//单独处理 p$s = preg_replace('/<p\s*align="*?(right|left|center)"*?.*?>/i','<p style="max-width:90%">',$s);echo $s;
//去掉不允许的 tag$s = strip_tags($s,'<p>,<a>,<img alt="求关于正则表达式PHP过滤编辑器的新闻内容" >,<strong>,<em>');//去掉tag 里面的属性$s = preg_replace('/<(strong|em)[^>]+?>/i','<$1>',$s);//img 和 a 单独处理$s = preg_replace('/<(a|img).+?(href|src)="([^"]+?)"[^>]*?>/i','<$1 $2="$3">',$s);//单独处理 p$s = preg_replace('/<p\s*align="*?(right|left|center)"*?.*?>/i','<p style="max-width:90%">',$s);echo $s; 很给力,谢谢!










