note 91346 modified in function.strip-tags by danbrown

From: Date: Mon, 08 Jun 2009 12:38:39 +0000
Subject: note 91346 modified in function.strip-tags by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156453@lists.php.net to get a copy of this message
Here's a function that verify if in a string have broken HTML tags, returning true if do. Useful for previning broken tags to affect the page. <?php # Example usage: broken_tags("<b>This is a string</u>") returns TRUE, broken_tags("<b>This is a string</b>") returns FALSE. function broken_tags($str) { preg_match_all("/(<\w+)(?:.){0,}?>/", $str, $v1); preg_match_all("/<\/\w+>/", $str, $v2); $open = array_map('strtolower', $v1[1]); $closed = array_map('strtolower', $v2[0]); foreach ($open as $tag) { $end_tag = preg_replace("/<(.*)/", "</$1>", $tag); if (!in_array($end_tag, $closed)) return true; unset($closed[array_search($end_tag, $closed)]); } return false; } ?> --was-- Here's a function that verify if in a string have broken HTML tags, returning true if do. Useful for previning broken tags to affect the page. # Example usage: broken_tags("<b>This is a string</u>") returns TRUE, broken_tags("<b>This is a string</b>") returns FALSE. function broken_tags($str) { preg_match_all("/(<\w+)(?:.){0,}?>/", $str, $v1); preg_match_all("/<\/\w+>/", $str, $v2); $open = array_map('strtolower', $v1[1]); $closed = array_map('strtolower', $v2[0]); foreach ($open as $tag) { $end_tag = preg_replace("/<(.*)/", "</$1>", $tag); if (!in_array($end_tag, $closed)) return true; unset($closed[array_search($end_tag, $closed)]); } return false; } http://php.net/manual/en/function.strip-tags.php

« previous php.notes (#156453) next »