note 92008 added to function.htmlentities

From: Date: Sat, 04 Jul 2009 19:16:27 +0000
Subject: note 92008 added to function.htmlentities
Groups: php.notes 
Request: Send a blank email to php-notes+get-157925@lists.php.net to get a copy of this message
under what circumstances would someone want a ntilde [ñ] to be converted into "ñ" as htmlentities does? the correct method of translation should return the accurate NCR for the multibyte unicode sequence which in this case is &#241; *This must to be posted* as I personally know of many developers that deal with tons of frustration due to this shortcoming. I've already posted this twice but for some reason it hasn't made it through. perhaps this time. <?php //simple task: convert everything from utf-8 into an NCR[numeric character reference] class unicode_replace_entities { public function UTF8entities($content="") { $contents = $this->unicode_string_to_array($content); $swap = ""; $iCount = count($contents); for ($o=0;$o<$iCount;$o++) { $contents[$o] = $this->unicode_entity_replace($contents[$o]); $swap .= $contents[$o]; } return mb_convert_encoding($swap,"UTF-8"); //not really necessary, but why not. } public function unicode_string_to_array( $string ) { //taken from mb_split page...adjwilli $strlen = mb_strlen($string); while ($strlen) { $array[] = mb_substr( $string, 0, 1, "UTF-8" ); $string = mb_substr( $string, 1, $strlen, "UTF-8" ); $strlen = mb_strlen( $string ); } return $array; } //reworked slightly from Miguel Perez post on ord page public function unicode_entity_replace($c) { $h = ord($c{0}); if ($h <= 0x7F) { return $c; } else if ($h < 0xC2) { return $c; } if ($h <= 0xDF) { $h = ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F); $h = "&#" . $h . ";"; return $h; } else if ($h <= 0xEF) { $h = ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6 | (ord($c{2}) & 0x3F); $h = "&#" . $h . ";"; return $h; } else if ($h <= 0xF4) { $h = ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12 | (ord($c{2}) & 0x3F) << 6 | (ord($c{3}) & 0x3F); $h = "&#" . $h . ";"; return $h; } } }// //utf-8 environment $content = "<strong>baño baño baño</strong>日本語 = nihongo da ze.<br />"; $oUnicodeReplace = new unicode_replace_entities(); $content = $oUnicodeReplace->UTF8entities($content); echo "<br />Result:<br />"; echo $content; $source = htmlentities($content); echo "<br />htmlentities of resulting data:<br />"; echo $source; echo "<br /><br />Note: Entities get replaced with 'literals' in textarea FF3<br /><br />"; echo "<textarea style='width:300px;height:150px;'>"; echo $content; echo "</textarea>"; echo "<br /><br />For editing NCR's rather than 'literals' in a textarea<br /><br />"; echo "<textarea style='width:300px;height:150px;'>"; echo preg_replace("/(&#)+/","&amp;#",$content); echo "</textarea>"; ---- Server IP: 209.41.74.194 Probable Submitter: 76.14.99.95 ---- Manual Page -- http://www.php.net/manual/en/function.htmlentities.php Edit -- https://master.php.net/note/edit/92008 Del: integrated -- https://master.php.net/note/delete/92008/integrated Del: useless -- https://master.php.net/note/delete/92008/useless Del: bad code -- https://master.php.net/note/delete/92008/bad+code Del: spam -- https://master.php.net/note/delete/92008/spam Del: non-english -- https://master.php.net/note/delete/92008/non-english Del: in docs -- https://master.php.net/note/delete/92008/in+docs Del: other reasons-- https://master.php.net/note/delete/92008 Reject -- https://master.php.net/note/reject/92008 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#157925) next »