<?php
$html = <<< HTML
<A href="http://www.google.com" target="_blank" alt="Google Search">Google搜索</A>
<P align="right">abc</P>
<FONT color="#ffffff" face="Simsun" class="c12">测试</FONT>
HTML;function clean_html($code) {
  $p[] = "/(<a).*((?:href|target)=([\"']).+\\3).*>/iU";
  $r[] = "$1 $2>";
  $p[] = "/(<p).*(align=([\"']).+\\3).*>/iU";
  $r[] = "$1 $2>";
  $p[] = "/(<font).*(color=([\"']).+\\3).*>/iU";
  $r[] = "$1 $2>";
  return preg_replace($p,$r,$code);
}echo clean_html($html);
?>

<A href="http://www.google.com">Google搜索</A>
<P align="right">abc</P>
<FONT color="#ffffff">测试</FONT>

解决方案 »

  1.   

    给你一个解析属性的类
    /**********************************************
    //class DedeAttribute DedeCms模板标记属性集合
    **********************************************/
    //属性的数据描述
    class DedeAttribute
    {
           var $Count = -1;
           var $Items = ""; //属性元素的集合
           //
           //获得某个属性
           //
           function GetAtt($str)
           {
            if(!empty($this->Items[$str])) return  $this->Items[$str];
                else return "";
           }
           //同上
           function GetAttribute($str)
           {
            return $this->GetAtt($str);
           }
           //
           //判断属性是否存在
           //
           function IsAttribute($str)
           {
            if(!empty($this->Items[$str])) return true;
                else return false;
           }
           //
           //获得标记名称
           //
           function GetTagName()
           {
             return $this->GetAtt("tagname");
           }
           //
           // 获得属性个数
           //
           function GetCount()
       {
    return $this->Count+1;
       }
    }
    //
    //属性解析器
    //
    class DedeAttributeParse
    {
    var $SourceString = "";
    var $SourceMaxSize = 1024;
    var $CAttribute = ""; //属性的数据描述类
    //////设置属性解析器源字符串////////////////////////
    function SetSource($str="")
    {
            $this->CAttribute = new DedeAttribute();
    //////////////////////
    $strLen = 0;
    $this->SourceString = trim(ereg_replace("[ /\t\r\n]{1,}"," ",$str));
    $strLen = strlen($this->SourceString);
    if($strLen>0&&$strLen<=$this->SourceMaxSize)
    {
    //仅当源字符非空和不长于最大限定值时才解析属性
    $this->parSource();
    }
    }
    //////解析属性(私有成员,仅给SetSource调用)/////////////////
    function ParSource()
    {
    $d = "";
    $tmpatt="";
    $tmpvalue="";
    $startdd=-1;
    $ddtag="";
    $notAttribute=true;
    $strLen = strlen($this->SourceString);
    // 这里是获得Tag的名称,可视情况是否需要
    // 如果不在这个里解析,则在解析整个Tag时解析
    // 属性中不应该存在tagname这个名称
    for($i=0;$i<$strLen;$i++)
    {
    $d = substr($this->SourceString,$i,1);
    if($d==' ')
    {
    $this->CAttribute->Count++;
    $this->CAttribute->Items["tagname"]=strtolower(trim($tmpvalue));
    $tmpvalue = "";
    $notAttribute = false;
    break;
    }
    else
    $tmpvalue .= $d;
    }
    //不存在属性列表的情况
    if($notAttribute)
    {
    $this->CAttribute->Count++;
    $this->CAttribute->Items["tagname"]=strtolower(trim($tmpvalue));
    }
    //如果字符串含有属性值,遍历源字符串,并获得各属性
    if(!$notAttribute){
    for($i;$i<$strLen;$i++)
    {
    $d = substr($this->SourceString,$i,1);
    if($startdd==-1)
    {
    if($d!="=") $tmpatt .= $d;
    else
    {
    $tmpatt = strtolower(trim($tmpatt));
    $startdd=0;
    }
    }
    else if($startdd==0)
    {
    switch($d)
    {
    case ' ':
    continue;
    break;
    case '\'':
    $ddtag='\'';
    $startdd=1;
    break;
    case '"':
    $ddtag='"';
    $startdd=1;
    break;
    default:
    $tmpvalue.=$d;
    $ddtag=' ';
    $startdd=1;
    break;
    }
    }
    else if($startdd==1)
    {
    if($d==$ddtag)
    {
    $this->CAttribute->Count++;
                        $this->CAttribute->Items[$tmpatt]=strtolower(trim($tmpvalue));
    $tmpatt = "";
    $tmpvalue = "";
    $startdd=-1;
    }
    else
    $tmpvalue.=$d;
    }
    }
    if($tmpatt!="")
    {
    $this->CAttribute->Count++;
    $this->CAttribute->Items[$tmpatt]=strtolower(trim($tmpvalue));
    }
    //完成属性解析
    }//for
    }//has Attribute
    }
      

  2.   

    <?
    $str = '<A href="http://www.google.com" target="_blank" alt="Google Search">Google搜索</A>
    <P align="right">abc</P>
    <FONT color="#ffffff" face="Simsun" class="c12">测试</FONT>';
    function parsehtml($str){
    $str = preg_replace("/(<A).*(href=\".*\").*(target=\".*\").*(>.*<\/A>)/isU", "$1 $2 $3 $4", $str);
    $str = preg_replace("/(<P).*(align=\".*\").*(>.*<\/P>)/isU", "$1 $2 $3", $str);
    $str = preg_replace("/(<FONT).*(color=\".*\").*(>.*<\/FONT>)/isU", "$1 $2 $3", $str);
    return $str;
    }
    echo parsehtml($str);
    ?>
    楼主,你怎么还在这里问?
    我给你的这个不行吗?
      

  3.   

    kingerq(多菜鸟)兄。你的正则表达式是正确的。但是如果我的属性有的有双引号,有的没有。那怎么办呢?