note 107696 deleted from function.mb-convert-encoding by danbrown
| From: | danbrown@php.net | Date: | Mon, 27 Feb 2012 15:48:24 +0000 |
| Subject: | note 107696 deleted from function.mb-convert-encoding by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-186386@lists.php.net to get a copy of this message | ||
Note Submitter: phpdocs at jeudi dot de
----
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