- 论坛徽章:
- 0
|
[code] HTML_fixed,自动修补HTML代码片段
en,的确有bug,谢谢指出,现已做进一步修正,代码如下:
- function html_fixed($buf) {
- $len = strlen($buf);
-
- $tag_open = false;
- $tag_alone = Array("br", "meta", "base", "hr", "!", "img", "input");
- $tag_stack = Array();
- $tag_tmp = "";
- $ret = "";
- while ($i < $len) {
- $ch = $buf{$i};
- $i++;
- if ($ch == '<') {
- $tag_tmp = "";
- // 之前的标签没关先关闭
- if ($tag_open == true) {
- $ret .= ">;";
- }
-
- $ret .= $ch;
- // 打开标签
- $tag_open = true;
- // get tag_flag
- while ($i < $len) {
- $ch = $buf{$i};
- $i++;
- if ($ch == '<') {
- $ret .= ">;";
- $tag_open = false;
- $i--;
- }
- else {
- $ret .= $ch;
- }
- if ($ch == '>;') $tag_open = false;
- // tag_end
- if (strchr(" \t\r\n->;<", $ch))
- break;
- $tag_tmp .= $ch;
- }
- // get tag_flag end
- // after get tag
- if (!empty($tag_tmp)) {
- $tag_tmp = strtolower($tag_tmp);
- if (substr($tag_tmp, 0, 1) == "/") {
- // 闭合型标签
- $tmp_stack = Array();
- while ($tmp = array_pop($tag_stack)) {
- if (!strcmp($tmp, $tag_tmp))
- break;
- array_push($tmp_stack, $tmp);
- }
- while ($tmp = array_pop($tmp_stack)) {
- array_push($tag_stack, $tmp);
- }
- }
- else if (!in_array($tag_tmp, $tag_alone)) {
- // 存入堆栈待关闭
- array_push($tag_stack, $tag_tmp);
- }
- }
- }
- else {
- if ($ch == '>;') {
- // 关闭标签
- $tag_open = false;
- }
- $ret .= $ch;
- }
- } // end while
- // 最后看看 tag_open ?? 和 tag_stack ??
- if ($tag_open) {
- $ret .= ">;";
- }
- while ($tmp = array_pop($tag_stack)) {
- $ret .= "</{$tmp}>;";
- }
- $ret .= "\n";
- return $ret;
- }
复制代码 |
|