note 37983 added to function.htmlentities

From: Date: Wed, 03 Dec 2003 21:27:00 +0000
Subject: note 37983 added to function.htmlentities
Groups: php.notes 
Request: Send a blank email to php-notes+get-61361@lists.php.net to get a copy of this message
This is a different approach to convert html entities while honoring tags. Given the following: $string = "this is <b>a test</b> for tags & stuff like < this and <> that"; Now you want to have the &, <, > encoded while "a test" should remain in bold. Dodge this: $encoded = implode('', preg_replace('/^([^<].+[^>])$/e', "htmlentities('\\1')", preg_split('/(<.+?>)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE))); It returns: $encoded = "this is <b>a test</b> for tags &amp; stuff like < this and <> that"; This way, < and > not belonging to tags ARE encoded - unlike with the larger code sample a little further down these notes. ---- Manual Page -- http://www.php.net/manual/en/function.htmlentities.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37983 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37983&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37983&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#61361) next »