note 49332 modified in function.htmlentities by mazzanet
| From: | mazzanet@php.net | Date: | Mon, 03 Oct 2005 08:57:38 +0000 |
| Subject: | note 49332 modified in function.htmlentities by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-96244@lists.php.net to get a copy of this message | ||
htmlEncodeText (below) needs a small tweak, the dash needs to be made literal to get picked up in
cases like '<a href="blah-blah.php">'. I have been using this function to
parse my postgresql database calls since I have alot of unicode data and I don't want HTML data
to be neutered (via htmlentities()).
<?php
function htmlEncodeText ($string)
{
$pattern = '<([a-zA-Z0-9\.\, "\'_\/\-\+~=;:\(\)?&#%![\]@]+)>';
preg_match_all ('/' . $pattern . '/', $string, $tagMatches, PREG_SET_ORDER);
$textMatches = preg_split ('/' . $pattern . '/', $string);
foreach ($textMatches as $key => $value) {
$textMatches [$key] = htmlentities ($value);
}
for ($i = 0; $i < count ($textMatches); $i ++) {
$textMatches [$i] = $textMatches [$i] . $tagMatches [$i] [0];
}
return implode ($textMatches);
}
?>
--Editor note: Combined some corrections to the regex pattern, thanks to fabian dot lange at web dot
de, hammertscrew at veryweb dot com, webmaster AT scholesmafia DOT co DOT uk, thomas AT cosifan DOT
de and marques at displague dot com---
--was--
htmlEncodeText (below) needs a small tweak, the dash needs to be made literal to get picked up in
cases like '<a href="blah-blah.php">'. I have been using this function to
parse my postgresql database calls since I have alot of unicode data and I don't want HTML data
to be neutered (via htmlentities()).
<?php
function htmlEncodeText ($string)
{
$pattern = '<([a-zA-Z0-9\. "\'_\/\-=;\(\)?&#%]+)>';
preg_match_all ('/' . $pattern . '/', $string, $tagMatches, PREG_SET_ORDER);
$textMatches = preg_split ('/' . $pattern . '/', $string);
foreach ($textMatches as $key => $value) {
$textMatches [$key] = htmlentities ($value);
}
for ($i = 0; $i < count ($textMatches); $i ++) {
$textMatches [$i] = $textMatches [$i] . $tagMatches [$i] [0];
}
return implode ($textMatches);
}
?>
http://php.net/manual/en/function.htmlentities.php