2013年5月22日 星期三

補齊HTML Tag

最近同事在修正使用者輸入 HTML ,但有不完整的狀況,要將 HTML Tag 補齊

例如:

input : <font color="#f00">aaaa<b>aaaaaaaaaaa
output : <font color="#f00">aaaa<b>aaaaaaaaaaa</b></font>



同事分享了一個網址的 Sample Code (不過有 Bug ) ,Bug如下:

input : <font color="#f00">aaaa<b>aaaaaaaaaaa</font>
output : <font color="#f00">aaaa<b>aaaaaaaaaaa</font></b>
巢狀對應不起來 (有空再來修這個 Bug )



/** * close all open xhtml tags at the end of the string
 * * @param string $html
 * @return string
 * @author Milian 
 */

function closetags($html) {

  #put all opened tags into an array
  preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
  $openedtags = $result[1];   #put all closed tags into an array
  preg_match_all('#</([a-z]+)>#iU', $html, $result);
  $closedtags = $result[1];
  $len_opened = count($openedtags);

  # all tags are closed
  if (count($closedtags) == $len_opened) {
    return $html;
  }

  $openedtags = array_reverse($openedtags);

  # close tags
  for ($i=0; $i < $len_opened; $i++) {
    if (!in_array($openedtags[$i], $closedtags)){
      $html .= '</'.$openedtags[$i].'>';
    } else {
      unset($closedtags[array_search($openedtags[$i], $closedtags)]);    }
  }  
  return $html;
  
}  

再来修

同事版本 fix.

<?php
$str = '<marquee><font value="1222">A</font><br><font value="xxx">B' ;

_fixBlockHTML($str) ;

function StackProcess($strTag, &$arrTagNameStack, &$strTmp)
{
    if (!strpos($strTag, '/'))
    {
        if (!stripos($strTag, "br"))
        {
            preg_match_all('#<([a-zA-z]+)(?: .*)?(?<![/|/ ])>#iU', $strTag, $arrResult) ;
            $arrTagNameStack[] = $arrResult[1][0] ;
        }
    }
    else
    {
        if (count($arrTagNameStack) > 0)
        {
            preg_match_all('#</([a-zA-z]+)>#iU', $strTag, $arrResult) ;
            $strClosedTagName = $arrResult[1][0] ;
            $strTopTagName = array_pop($arrTagNameStack) ;

            if (strcasecmp($strClosedTagName, $strTopTagName))
            {
                $strTmp = substr_replace($strTmp, "</".$strTopTagName.">", strpos($strTmp, "</".$strClosedTagName.">"), 0) ;
                StackProcess($strTag, $arrTagNameStack, $strTmp) ;
            }
        }
    }
}

function _fixBlockHTML($strTmp)
{
    preg_match_all('#</?([a-zA-z]+)(?: .*)?(?<![/|/ ])>#iU', $strTmp, $arrResult) ;
    $arrAllTags = $arrResult[0] ;

    $arrTagNameStack = array() ;

    foreach ($arrAllTags as $tag)
    {
        StackProcess($tag, $arrTagNameStack, $strTmp) ;
    }

    if (count($arrTagNameStack) > 0)
    {
        $arrTagNameStack = array_reverse($arrTagNameStack) ;

        foreach($arrTagNameStack as $tag)
        {
            $strTmp .= '</'.$tag.'>' ;
        }
    }

    echo $strTmp ;
}
?>


沒有留言:

張貼留言