note 61205 added to function.html-entity-decode

From: Date: Sat, 28 Jan 2006 03:13:39 +0000
Subject: note 61205 added to function.html-entity-decode
Groups: php.notes 
Request: Send a blank email to php-notes+get-102597@lists.php.net to get a copy of this message
To decode characters within a string that have been encoded to their HTML character code (in the format &#xx;), use the following function unicode_decode(). A list of HTML character codes can be found at: http://www.ascii.cl/htmlcodes.htm Note: To encode a string to HTML character codes (in the format &#xx;), use the function unicode_entities(), listed in the comments of http://php.net/manual/function.htmlentities.php <?php /** * Decode each char in a string from its HTML code. * Characters denoded by this function were encoded * by unicode_entities(). * * This function will work with PHP >= 3.0.9, 4, 5. * * @author Tim Burgan <timburgan@gmail.com> * @version 1.0.0 * @link http://timburgan.com/ * @link http://php.net/manual/function.html-entity-decode.php * @see http://php.net/manual/function.htmlentities.php * @param string $string String to decode * @return string Returns a string of decoded chars */ function unicode_decode($string) { // declare variables $tok = 0; $cur = 0; $chars = null; // move through the string until the end is reached while ( $cur < strlen($string) ) { // find the next token $tok = strpos($string, "&#", $cur); // if no more tokens exist, move pointer to end of string if ( $tok === false ) $tok = strlen($string); // if the current char is alpha-numeric or a space if ( preg_match("/^(w| )$/",substr($string, $cur, 1)) ) { $chars .= substr($string, $cur, $tok - $cur); } // the current char must be the start of a token else { $cur += 2; $tok = strpos($string, ';', $cur); $chars .= chr(substr($string, $cur, $tok - $cur)); $tok++; } // move the current pointer to the next token $cur = $tok; } return $chars; } ?> Usage: <?php $string = "&#60;a href&#61;&#34;http&#58;&#47;&#47;timburgan&#46;com&#34; rel&#61;&#34;external&#34; title&#61;&#34;Go to timburgan&#46;com&#34;&#62;Tim Burgan&#60;&#47;a&#62;"; echo unicode_decode($string); ?> Output: <a href="http://timburgan.com" rel="external" title="Go to timburgan.com">Tim Burgan</a> ---- Server IP: 203.220.0.235 Probable Submitter: 203.26.206.129 (proxied: 203.122.231.20) ---- Manual Page -- http://www.php.net/manual/en/function.html-entity-decode.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61205 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61205&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61205&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61205&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61205&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61205&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61205&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61205&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61205&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102597) next »