note 69412 modified in function.mb-convert-encoding by danbrown

From: Date: Mon, 27 Feb 2012 15:48:21 +0000
Subject: note 69412 modified in function.mb-convert-encoding by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-186385@lists.php.net to get a copy of this message
I\&#039;d like to share some code to convert latin diacritics to their traditional 7bit representation, like, for example, - &agrave;,&ccedil;,&eacute;,&icirc;,... to a,c,e,i,... - &szlig; to ss - &auml;,&Auml;,... to ae,Ae,... - &euml;,... to e,... (mb_convert \&quot;7bit\&quot; would simply delete any offending characters). I might have missed on your country\&#039;s typographic conventions--correct me then. &lt;?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 = mb_convert_encoding($text,\&#039;HTML-ENTITIES\&#039;,$from_enc); $text = preg_replace( array(\&#039;/&szlig;/\&#039;,\&#039;/&amp;(..)lig;/\&#039;, \&#039;/&amp;([aouAOU])uml;/\&#039;,\&#039;/&amp;(.)[^;]*;/\&#039;), array(\&#039;ss\&#039;,\&quot;$1\&quot;,\&quot;$1\&quot;.\&#039;e\&#039;,\&quot;$1\&quot;), $text); return $text; } ?&gt; Enjoy :-) Johannes == [EDIT BY danbrown AT php DOT net: Author provided the following update on 27-FEB-2012.] == An addendum to my &quot;to7bit&quot; 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 &szlig; ligature is usually written &quot;ss&quot; in 7bit context. Dutch &yuml; is typically rendered &quot;ij&quot; (not &quot;y&quot;). The original function works well with word (alphabet) character entities and I&#039;ve seen it used in many places. But non-word entities cause funny results: E.g., &quot;&copy;&quot; is rendered as &quot;c&quot;, &quot;&shy;&quot; as &quot;s&quot; and &quot;&amp;rquo;&quot; as &quot;r&quot;. The following version fixes this by converting non-alphanumeric characters (also chains thereof) to &#039;_&#039;. &lt;?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+/,&#039;_&#039;,$text); $text = mb_convert_encoding($text,&#039;HTML-ENTITIES&#039;,$from_enc); $text = preg_replace( array(&#039;/&szlig;/&#039;,&#039;/&amp;(..)lig;/&#039;, &#039;/&amp;([aouAOU])uml;/&#039;,&#039;/&yuml;/&#039;,&#039;/&amp;(.)[^;]*;/&#039;), array(&#039;ss&#039;,&quot;$1&quot;,&quot;$1&quot;.&#039;e&#039;,&#039;ij&#039;,&quot;$1&quot;), $text); return $text; } ?&gt; Enjoy again, Johannes --was-- I'd like to share some code to convert latin diacritics to their traditional 7bit representation, like, for example, - à,ç,é,î,... to a,c,e,i,... - ß to ss - ä,Ä,... to ae,Ae,... - ë,... to e,... (mb_convert "7bit" would simply delete any offending characters). I might have missed on your country's typographic conventions--correct me then. <?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 = mb_convert_encoding($text,'HTML-ENTITIES',$from_enc); $text = preg_replace( array('/&szlig;/','/&(..)lig;/', '/&([aouAOU])uml;/','/&(.)[^;]*;/'), array('ss',"$1","$1".'e',"$1"), $text); return $text; } ?> Enjoy :-) Johannes http://php.net/manual/en/function.mb-convert-encoding.php

« previous php.notes (#186385) next »