note 98027 deleted from function.htmlentities by danbrown

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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;

}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.