-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
36 lines (28 loc) · 883 Bytes
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
/**
* close all open xhtml tags at the end of the string
* @param string $html
* @return string
* @author Milian <[email protected]>
*/
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;
}