note 91925 deleted from function.ord by danbrown
| From: | danbrown@php.net | Date: | Wed, 01 Jul 2009 20:09:04 +0000 |
| Subject: | note 91925 deleted from function.ord by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157791@lists.php.net to get a copy of this message | ||
Note Submitter: montana [at] percepticon [dot] com
----
<?php
class unicode_replace_entities {
public function UTF8entities($content="") { //@todo = run aggressive tests on this
$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 php.net page...adjwilli
at yahoo dot com
$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 @ http://us.php.net/manual/en/function.ord.php
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 />";
//test various approaches to maintaining characters...
$result = htmlentities($content);
echo "htmlentities: ugh, not really what we're looking for:";
echo $result; //FAIL
$result = htmlspecialchars($content);
echo "<br /><br />";
echo "htmlspecialcharacters: nope:";
echo $result; //FAIL
//mb convert encoding doesn't connect entities at all so that definitely won't work.
//what I want is for the characters to be maintained regardless of what the user enters...
$oUnicodeReplace = new unicode_replace_entities();
$content = $oUnicodeReplace->UTF8entities($content);
echo "<br /><br />unicode_replace_entities: this will work:<br /><br
/>";
echo $content;
echo "<br /><br />Hmmmn, Entities get replaced with 'literals' in
textarea FF3<br /><br />";
echo "<textarea style='width:300px;height:150px;'>";
echo $content; //still can't really submit this into the db like this
echo "</textarea>";
//for editing NCR's in a textarea
echo "<br /><br />";
echo "hack to maintain NCR's in textarea<br />";
echo "<textarea style='width:300px;height:150px;'>";
echo preg_replace("/(&#)+/","&#",$content);
echo "</textarea>";
//@marlonx80 at hotmail dot com = that utf-8 stuff doesn't work any better than htmlentities
for me...
//montana