能用PHP 去掉所有html标签里的部分属性吗?只要保留<a>标签里的超链接即可!

比如:<divid="m"><pid="lg"><imgsrc="../img/baidu_sylogo1.gif"width="27px"height="12px"us... 比如:
<div id="m"><p id="lg"><img src="../img/baidu_sylogo1.gif" width="27px" height="12px" usemap="#mp"><map name="mp"><a shape="rect" coords="40,25,230,95" href="../yuanso/index.html" target="_blank" title="点此进入空间" ></map></p><p id="nv"><a href="../yuanso/index1.html">文字1</a> <b>文字2</b> <a href="../yuanso/index3.html">文字3</a></p></div>

去除后只剩下:
<div><p><img><map><a href="../yuanso/index.html"></map></p><p><a href="../yuanso/index1.html">文字1</a> <b>文字2</b> <a href="../yuanso/index3.html">文字3</a></p></div>
嗯解决了,还是自已研究的好,现在把代码贴出来与大家分享(不像有些人测都没测试,就贴出来;还说浪费自已时间呢;那我问你于吗要浪费时间发这贴呢,好笑,太好笑了,哈哈!!!!!!!!!!!1)!
<?php
$file='<div id="m"><p id="lg"><img src="../img/baidu_sylogo1.gif" width="27px" height="12px" usemap="#mp"><map name="mp"><a shape="rect" coords="40,25,230,95" href="../yuanso/index.html" target="_blank" title="点此进入空间" ></map></p><p id="nv"><a href="../yuanso/index1.html">文字1</a> <b>文字2</b> <a href="../yuanso/index3.html">文字3</a></p></div>
';
$del=array("/name=.+?['|\"]/i","/src=.+?['|\"]/i","/id=.+?['|\"]/i","/width=.+?['|\"]/i","/height=.+?['|\"]/i","/usemap=.+?['|\"]/i","/shape=.+?['|\"]/i","/coords=.+?['|\"]/i","/target=.+?['|\"]/i","/title=.+?['|\"]/i");
$file = preg_replace($del,"",$file);//去除style样式
$file = str_replace(" ","",$file);//去除所有空格
$file = str_replace("<ahref=","<a href=",$file);//还原空格
echo $file;
?>
展开
 我来答
家养冠毛犬
2012-05-17 · 超过12用户采纳过TA的回答
知道答主
回答量:29
采纳率:0%
帮助的人:29.1万
展开全部
<?php
$file='<div id="m"><p id="lg"><img src="../img/baidu_sylogo1.gif" width="27px" height="12px" usemap="#mp"><map name="mp"><a shape="rect" coords="40,25,230,95" href="../yuanso/index.html" target="_blank" title="点此进入空间" ></map></p><p id="nv"><a href="../yuanso/index1.html">文字1</a> <b>文字2</b> <a href="../yuanso/index3.html">文字3</a></p></div>
';
$del=array("/name=.+?['|\"]/i","/src=.+?['|\"]/i","/id=.+?['|\"]/i","/width=.+?['|\"]/i","/height=.+?['|\"]/i","/usemap=.+?['|\"]/i","/shape=.+?['|\"]/i","/coords=.+?['|\"]/i","/target=.+?['|\"]/i","/title=.+?['|\"]/i");
$file = preg_replace($del,"",$file);//去除style样式
$file = str_replace(" ","",$file);//去除所有空格
$file = str_replace("<ahref=","<a href=",$file);//还原空格
echo $file;
?>
注册好勒
2012-05-02 · TA获得超过397个赞
知道小有建树答主
回答量:497
采纳率:0%
帮助的人:331万
展开全部
//注释掉你想保存的
function htmltype($content)
{
if(!empty($content))
{
$replaces = array("/<style.*?>.*<\/style>/i" => '',
/*"/<img.*?>/i" => '',*/
"/<font.*?>/i" => '',
"/<\/font>/i" => '',
"/<strong>/i" => '',
"/<\/strong>/i" => '',
"/<span.*?>/i" => '',
"/<\/span>/i" => '',
"/<div.*?>/i" => "<p>",
"/<\/div>/i" => "</p>",
"/<table.*?>/i" => '',
"/<\/table>/i" => '',
"/<tbody.*?>/i" => '',
"/<\/tbody>/i" => '',
"/<tr.*?>/i" => '<p>',
"/<\/tr>/i" => '</p>',
"/<td.*?>/i" => '',
"/<\/td>/i" => '',
"/<ul.*?>/i" => "<p>",
"/<\/ul>/i" => "</p>",
"/<li.*?>/i" => "<p>",
"/<\/li>/i" => "</p>",
"/<h[1-6]+.*?>/i" => "<p>",
"/<\/h[1-6]+?>/i" => "</p>",
"/<a.*?>/i" => '',
"/<\/a>/i" => '',
"/<br(.*)?>/i" => "</p></p>",
"/<b>/i" => '',
"/<\/b>/i" => '',
"/<script.*?>.*<\/script>/i" => '',
"/<iframe.*?>.*<\/iframe>/i" => '',
"/style=\".*?\"/i" => '',
"/style=\'.*?\'/i" => '',
"/ /i" => '',
"/<p.*?>/i" => '<p>',
"/[( )|( )]/u" => '',
"/[\r|\n]+/" => '',
"/<p>(<p>)+/i" => '<p>',
"/<\/p>(<\/p>)+/i" => '</p>',
"/(<p><\/p>)+/i" => '',
"/<p>/i" => '<p>  ',
);

foreach($replaces as $k => $v)
$content = preg_replace($k,$v,$content);
}
return $content;
}
更多追问追答
追问
不行呵,你乱写吧?
追答
跟你这样的有什么好说的?真浪费我时间
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
myfirtyou
2012-05-03
知道答主
回答量:49
采纳率:0%
帮助的人:22.4万
展开全部
楼上已经回答得很清楚了
追问
你测试了吗,你不会也是想来拿分的吧
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
丶一息若存
2012-05-10 · 贡献了超过126个回答
知道答主
回答量:126
采纳率:0%
帮助的人:33万
展开全部
任务飘过
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式