从网站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">
<a class="aritcle_card_img" href="/ai/2121">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175680148482336.png" alt="AI图像编辑器">
</a>
<div class="aritcle_card_info">
<a href="/ai/2121">AI图像编辑器</a>
<p>使用文本提示编辑、变换和增强照片</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="AI图像编辑器">
<span>46</span>
</div>
</div>
<a href="/ai/2121" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="AI图像编辑器">
</a>
</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>$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;
//去掉不允许的 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;
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号