關閉HTML的標籤

作者Nalivaeff 2009年5月5日

另一種刺激的 此時,我們關閉未關閉的html標記。 我不知道它可能會派得上用場 ...


function closetags($html) {
$single_tags = array('meta','img','br','link','area','input','hr','col','param','base');
preg_match_all('~< ([a-z0-9]+)(?: .*)?(? ~iU', $html, $result);
$openedtags = $result[1];
preg_match_all('~~iU', $html, $result);
$closedtags = $result[1];
$len_opened = count($openedtags);
if (count($closedtags) == $len_opened) {
return $html;
}
$openedtags = array_reverse($openedtags);
for ($i=0; $i < $len_opened; $i++) {
if (!in_array($openedtags[$i], $single_tags)) {
if (FALSE !== ($key = array_search($openedtags[$i], $closedtags))) {
unset($closedtags[$key]);
}
else {
$html .= ' ';
}
}
}
return $html;
}

function closetags($html) {
$single_tags = array('meta','img','br','link','area','input','hr','col','param','base');
preg_match_all('~< ([a-z0-9]+)(?: .*)?(? ~iU', $html, $result);
$openedtags = $result[1];
preg_match_all('~~iU', $html, $result);
$closedtags = $result[1];
$len_opened = count($openedtags);
if (count($closedtags) == $len_opened) {
return $html;
}
$openedtags = array_reverse($openedtags);
for ($i=0; $i < $len_opened; $i++) {
if (!in_array($openedtags[$i], $single_tags)) {
if (FALSE !== ($key = array_search($openedtags[$i], $closedtags))) {
unset($closedtags[$key]);
}
else {
$html .= ' ';
}
}
}
return $html;
}

function closetags($html) {
$single_tags = array('meta','img','br','link','area','input','hr','col','param','base');
preg_match_all('~< ([a-z0-9]+)(?: .*)?(? ~iU', $html, $result);
$openedtags = $result[1];
preg_match_all('~~iU', $html, $result);
$closedtags = $result[1];
$len_opened = count($openedtags);
if (count($closedtags) == $len_opened) {
return $html;
}
$openedtags = array_reverse($openedtags);
for ($i=0; $i < $len_opened; $i++) {
if (!in_array($openedtags[$i], $single_tags)) {
if (FALSE !== ($key = array_search($openedtags[$i], $closedtags))) {
unset($closedtags[$key]);
}
else {
$html .= ' ';
}
}
}
return $html;
}

窺視這裡

相關條目

分類:Skriptulki 標籤

評論

還沒有評論。


發表評論

(必填)

(必填)