i have this problem:
user can add an HTML description and it will be shown on his profile; when i show the user list, i'd wish to show this description, too.
since it could be too long, i'm going to cap it to a fixed lenght, but doing this i could break the HTML syntax leaving some tags open.
how can i check if everthing is ok and, if needed, close any open tag?
@tampe125 This isn't my code, but it looks like it works.
<?php /** * close all open xhtml tags at the end of the string
* * @param string $html
* @return string
* @author Milian <mail@mili.de>
*/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;} ?>
This is a simple javascript validator that does basic tag checking.
XML validator
The only issue is that it will stop at the first error, so you need to work through issues one at a time.