note 39667 added to function.preg-split
| From: | ed_isthmusNOSPAM at yahoo dot com | Date: | Fri, 06 Feb 2004 18:26:12 +0000 |
| Subject: | note 39667 added to function.preg-split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64738@lists.php.net to get a copy of this message | ||
I needed to encode special html characters in strings, but keep some of the tags working! This
function does the deed:
<?php
function html_out_keep_tags ($string) {
$newstring = '';
$pattern = '/(<\/?(?:a .*|h1|h2|b|i)>)/ims';
$newarray = preg_split( $pattern, $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
foreach ($newarray as $element) {
if (!preg_match($pattern, $element))
$element = htmlspecialchars ( html_entity_decode($element, ENT_QUOTES), ENT_QUOTES);
$newstring .= $element;
}
return $newstring;
}
?>
edit $pattern to change the allowed tags.
Note that ?: usefully prevents the sub-pattern from becoming a delimiter. Double encoding is
prevented, see notes on htmlspecialchars().
----
Manual Page -- http://www.php.net/manual/en/function.preg-split.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39667
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39667&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39667&report=yes
Search -- http://master.php.net/manage/user-notes.php