note 42143 added to function.htmlentities

From: Date: Wed, 05 May 2004 09:01:02 +0000
Subject: note 42143 added to function.htmlentities
Groups: php.notes 
Request: Send a blank email to php-notes+get-69222@lists.php.net to get a copy of this message
(Please ignore my post further down, it is buggy.) 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 &lt; this and &lt;&gt; that"; This way, < and > not belonging to tags are encoded. However, as this line of code does not actually check if a valid HTML keyword is between the < and >, the string "5 is < than 7 and not > 9" will not be encoded, as " than 7 and not " is falsely taken for a HTML tag. ---- Manual Page -- http://www.php.net/manual/en/function.htmlentities.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42143 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42143&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42143&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#69222) next »