note 98027 deleted from function.htmlentities by danbrown
| From: | danbrown@php.net | Date: | Fri, 21 May 2010 12:39:26 +0000 |
| Subject: | note 98027 deleted from function.htmlentities by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-169489@lists.php.net to get a copy of this message | ||
Note Submitter: Seb
----
I have recently built the function which converts all special characters from a given string, but
excapes all html tags and if there are already any valid special characters - it simply ignores
them.
Here it is:
function encodeHTML($string) {
$pattern = '<([a-zA-Z0-9\.\, "\'_\/\-\+~=;:\(\)?&#%![\]@]+)>';
// put text only, devided by html tags into array
$textMatches = preg_split('/' . $pattern . '/', $string);
// array for sanitised output
$textSanitised = array();
foreach($textMatches as $key => $value) {
$textSanitised[$key] = htmlentities(html_entity_decode($value, ENT_QUOTES, 'UTF-8'),
ENT_QUOTES, 'UTF-8');
}
foreach($textMatches as $key => $value) {
$string = str_replace($value, $textSanitised[$key], $string);
}
return $string;
}