note 107696 added to function.mb-convert-encoding
| From: | phpdocs at jeudi dot de | Date: | Mon, 27 Feb 2012 14:46:51 +0000 |
| Subject: | note 107696 added to function.mb-convert-encoding | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186384@lists.php.net to get a copy of this message | ||
An addendum to my "to7bit" function referenced below in the notes.
The function is supposed to solve the problem that some languages require a different 7bit rendering
of special (umlauted) characters for sorting or other applications. For example, the German Ã
ligature is usually written "ss" in 7bit context. Dutch ÿ is typically rendered
"ij" (not "y").
The original function works well with word (alphabet) character entities and I've seen it used
in many places. But non-word entities cause funny results:
E.g., "©" is rendered as "c", "­" as "s"
and "&rquo;" as "r".
The following version fixes this by converting non-alphanumeric characters (also chains thereof) to
'_'.
<?php
/**
* @args string $text line of encoded text
* string $from_enc (encoding type of $text, e.g. UTF-8, ISO-8859-1)
*
* @returns 7bit representation
*/
function to7bit($text,$from_enc) {
$text = preg_replace(/\W+/,'_',$text);
$text = mb_convert_encoding($text,'HTML-ENTITIES',$from_enc);
$text = preg_replace(
array('/ß/','/&(..)lig;/',
'/&([aouAOU])uml;/','/ÿ/','/&(.)[^;]*;/'),
array('ss',"$1","$1".'e','ij',"$1"),
$text);
return $text;
}
?>
Enjoy again,
Johannes
----
Server IP: 69.147.83.197
Probable Submitter: 87.172.158.224
----
Manual Page -- http://www.php.net/manual/en/function.mb-convert-encoding.php
Edit -- https://master.php.net/note/edit/107696
Del: integrated -- https://master.php.net/note/delete/107696/integrated
Del: useless -- https://master.php.net/note/delete/107696/useless
Del: bad code -- https://master.php.net/note/delete/107696/bad+code
Del: spam -- https://master.php.net/note/delete/107696/spam
Del: non-english -- https://master.php.net/note/delete/107696/non-english
Del: in docs -- https://master.php.net/note/delete/107696/in+docs
Del: other reasons-- https://master.php.net/note/delete/107696
Reject -- https://master.php.net/note/reject/107696
Search -- https://master.php.net/manage/user-notes.php