Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : String Functions : strtr

strtr

Translate certain characters (PHP 4, PHP 5)
string strtr ( string str, string from, string to )
string strtr ( string str, array replace_pairs )

Example 2472. strtr() example

<?php
$addr
= strtr($addr, "äåö", "aao");
?>

Example 2473. strtr() example with two arguments

<?php
$trans
= array("hello" => "hi", "hi" => "hello");
echo
strtr("hi all, I said hello", $trans);
?>

This will show:

hello all, I said hi

Code Examples / Notes » strtr

elonen

Yet another accent remover, this time pretty complete and without any 8-bit characters in the script itself:
$txt = strtr($txt,
"\xe1\xc1\xe0\xc0\xe2\xc2\xe4\xc4\xe3\xc3\xe5\xc5".
"\xaa\xe7\xc7\xe9\xc9\xe8\xc8\xea\xca\xeb\xcb\xed".
"\xcd\xec\xcc\xee\xce\xef\xcf\xf1\xd1\xf3\xd3\xf2".
"\xd2\xf4\xd4\xf6\xd6\xf5\xd5\x8\xd8\xba\xf0\xfa".
"\xda\xf9\xd9\xfb\xdb\xfc\xdc\xfd\xdd\xff\xe6\xc6\xdf",
"aAaAaAaAaAaAacCeEeEeEeEiIiIiIiInNoOoOoOoOoOoOoouUuUuUuUyYyaAs");


daijoubu

Wouldn't:
<?php
$s = str_replace(array_key($replace_array), array_value($replace_array), $s);
?>
be faster?
Perhaps even faster using 2 seperate arrays...


anonymous

Usefull remove Accent function:
return strtr($string,
"ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕ
ÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ",
"SOZsozYYuAAAAAAACEEEEIIIIDNOO
OOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy");


peter dot goodman

To the previous comment: great function, one character mapping it is missing is though is:
chr(226) => 'â'


persiflage2000

To replace unicode characters with their ascii equivalents, use
$my_string = strtr($my_string, $unicode_array);
using this array:
$unicode_array = array(
"&#8211;" => "-",
"&#8212;" => "-",
"–" => "-",
"&#8216;" => "'",
"’" => "'",
"&#8217;" => "'",
"‘" => "'",
"&#8230;" => "...",
"…" => "...",
"“" => "\"",
"&#8220;" => "\"",
"”" => "\"",
"&#8221;" => "\"",
);
It is important to note you need both the unicode contol character (&#8221;) AND its text equivalent (”).  For instance, text copied and pasted into your html form from M$ Word will have the control character, whereas the same text viewed in a browser and copied into your html form using drag-and-drop will have the text equivalent.
A pox on Redmond for the way M$ Word is set to convert all these characters to unicode equivalents by default.
Note: you can expand the array to get rid of accented characters too, but remember the same thing applies.  For example, both "&#352;" => "S" and "Š" => "S" should be added to the array.


m dot frank

to get the ascii equivalent of unicode characters simply use the

utf8_decode() function


gabi

To convert special chars to their html entities strtr you can use strtr in conjunction with get_html_translation_table(HTML_ENTITIES) :
$trans = get_html_translation_table(HTML_ENTITIES);
$html_code = strtr($html_code, $trans);
This will replace in $html_code the Á by &Aacute; , etc.


jamesheinrich

To build on the provided example, this small function will translate all accented characters into non-accented ones:
function removeaccents($string) {
return strtr($string, "ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ", "SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy");
}


derernst

This works for me to remove accents for some characters of Latin-1, Latin-2 and Turkish in a UTF-8 environment, where the htmlentities-based solutions fail:
<?php
function remove_accents($string, $german=false) {
   // Single letters
   $single_fr = explode(" ", "À Á Â Ã Ä Å &#260; &#258; Ç &#262; &#268; &#270; &#272; Ð È É Ê Ë &#280; &#282; &#286; Ì Í Î Ï &#304; &#321; &#317; &#313; Ñ &#323; &#327; Ò Ó Ô Õ Ö Ø &#336; &#340; &#344; Š &#346; &#350; &#356; &#354; Ù Ú Û Ü &#366; &#368; Ý Ž &#377; &#379; à á â ã ä å &#261; &#259; ç &#263; &#269; &#271; &#273; è é ê ë &#281; &#283; &#287; ì í î ï &#305; &#322; &#318; &#314; ñ &#324; &#328; ð ò ó ô õ ö ø &#337; &#341; &#345; &#347; š &#351; &#357; &#355; ù ú û ü &#367; &#369; ý ÿ ž &#378; &#380;");
   $single_to = explode(" ", "A A A A A A A A C C C D D D E E E E E E G I I I I I L L L N N N O O O O O O O R R S S S T T U U U U U U Y Z Z Z a a a a a a a a c c c d d e e e e e e g i i i i i l l l n n n o o o o o o o o r r s s s t t u u u u u u y y z z z");
   $single = array();
   for ($i=0; $i<count($single_fr); $i++) {
       $single[$single_fr[$i]] = $single_to[$i];
   }
   // Ligatures
   $ligatures = array("Æ"=>"Ae", "æ"=>"ae", "Œ"=>"Oe", "œ"=>"oe", "ß"=>"ss");
   // German umlauts
   $umlauts = array("Ä"=>"Ae", "ä"=>"ae", "Ö"=>"Oe", "ö"=>"oe", "Ü"=>"Ue", "ü"=>"ue");
   // Replace
   $replacements = array_merge($single, $ligatures);
   if ($german) $replacements = array_merge($replacements, $umlauts);
   $string = strtr($string, $replacements);
   return $string;
}
?>
I am sorry as the display of the user notes is broken the character lists shown will not be useable by now, but you see the principle. Speakers of certain languages might want to suggest changes or additions to the character lists.
Characters are divided by spaces and explode()d because I am on PHP4, and other splitting methods fail with double byte characters. In PHP5 split() might help, too.
The advantage of this approach is that non-latin characters remain untouched.


ktogias

This function is usefull for
accent insensitive regexp
searches into greek (iso8859-7) text:
(Select View -> Character Encoding -> Greek (iso8859-7)
at your browser to see the correct greek characters)
function gr_regexp($mystring){
       $replacement=array(
               array("¶","Á","á","Ü"),
               array("¸","Å","å","Ý"),
               array("¹","Ç","ç","Þ"),
               array("º","É","é","ß","ú","À"),
               array("¼","Ï","ï","ü"),
               array("¾","Õ","õ","ý","û","à"),
               array("¿","Ù","ù","þ")
       );
       foreach($replacement as $group){
               foreach($group as $character){
                       $exp="[";
                       foreach($group as $expcharacter){
                               $exp.=$expcharacter;
                       }
                       $exp.="]";
                       $trans[$character]=$exp;
               }
       }
       $temp=explode(" ", $mystring);
       for ($i=0;$i<sizeof($temp);$i++){
               $temp[$i]=strtr($temp[$i],$trans);
               $temp[$i]=addslashes($temp[$i]);
       }
       return implode(".*",$temp);
}
$match=gr_regexp("Óêïõëéêïìõñìéãêüôñõðá êáé ÔæéæéñéÜ");
//The next query string can be sent to MySQL
through mysql_query()
$query=
     "Select `column` from `table` where `column2` REGEXP  
                        '".$match."'";


stefan

there is obviously an S missing in getRewriteString from above:
function getRewriteString($sString) {
  $string    = htmlentities(strtolower($sString));
  $string    = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml);/", "$1", $string);
  $string    = preg_replace("/([^a-z0-9]+)/", "-", html_entity_decode($string));
  $string    = trim($string, "-");
  return $string;
}


ru dot dy

The function 'getWriteString' for parsing URLs below is very useful, but it has a little bug. PHP strtolower does not recognize capitalized accented characters. The result is that they are replaced with '-'. To correct this, change the first line in the function from:
<?php   $string = htmlentities(strtolower($string)); ?>
into:
<?php   $string = strtolower(htmlentities($sString)); ?>
Theres another Problem with search-engines, regarding german keywords with umlaute. Searchengines such as Google replace german umlaute with a following 'e'. Replacing the umlaute in words just with their unaccented characters gives the word another or no sense. The result of this is that the contained keyword in the url is incorrect and brings minor advantage for the pagerank of the site. At last, this did it for me:
<?php
function getRewriteString($sString) {
  $string = strtolower( htmlentities( strtr($sString, array("Ä"=>"Ae", "Ü"=>"Ue", "Ö"=>"Oe", "ä"=>"ae", "ü"=>"ue", "ö"=>"oe")) ) );
  $string = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml);/", "$1", $string);
  $string = preg_replace("/([^a-z0-9]+)/", "-", html_entity_decode($string));
  $string = trim($string, "-");
  return $string;
}
?>


marco dot colombo

Suppose you're trying to remove any character not in your set, i've found this very helpfull:
function my_remove($string, $my_set, $new=" ", $black="#")
{
 $first = strtr( $string, $my_set,
                   str_repeat($black, strlen($my_set)) );
 $second = strtr( $string, $first,
                   str_repeat($new, strlen($first)) );
 return $second;
};
NOTE that all non-wanted character will be replace with $new,
note also that $black must NOT to exist in $my_set.
Molok


herve

strtr() is useful when trying to convert a string from PHP to Javascript (as JS doesn't allow linebreaks in string declarations) :
<script language="Javascript">
<?php
print("var js_string = \"%s\";",strtr($php_string,"\n\r\t\0","    ");
?>


mykel

strtr is a usefull encoding mechinism instead of using str_rot13. you can impliment it when you write usernames to a file, for example. but know that it is easy to crack your encription.
an example:
<?php
$unencripted = "hello";
$from = "abcdefghijklmnopqrstuvwxyz";
$to =    "zyxwvutsrqponmlkjihgfedcba";
$temp = strtr($unencripted, $from, $to);
/* will return svool */
?>


jay k

Several people have suggested using str_replace instead of strtr... Here is why that will not always work....
<?
$string = "This is a BIG test with SMALL RESULT\n";
$repl = array('BIG' => 'SMALL', 'SMALL' => 'BIG');
echo str_replace(array_keys($repl), array_values($repl), $string);
echo strtr($string, $repl);
?>
Results in:
This is a BIG test with BIG RESULT
This is a SMALL test with BIG RESULT
Notice that str_replace will replace previous replacements and strtr will not.


oliver

Replace control characters in a binary string:
<?
function cc_replace($in) {
       for ($i = 0; $i <= 31; $i++) {
               $from  .= chr($i);
               $to    .= ".";
       }
       return strtr($in, $from, $to);
}
?>


volkris

Regarding christophe's conversion, note that the \x## values should be in double quotes, not single, so that the escape will be applied.

hotmail - marksteward

Referring to note from 11 October 2000, Thorn (Þ, þ), Eth (Ð, ð), Esset (ß) and Mu (µ) aren't really accented letters.  Œ, œ, Æ, æ are ligatures.  Best to do the following:
function removeaccents($string){
return strtr(
 strtr($string,
  'ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ',
  'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy'),
 array('Þ' => 'TH', 'þ' => 'th', 'Ð' => 'DH', 'ð' => 'dh', 'ß' => 'ss',
  'Œ' => 'OE', 'œ' => 'oe', 'Æ' => 'AE', 'æ' => 'ae', 'µ' => 'u'));
}
This would be no good for sorting, as thorn and eth aren't actually found under th and dh.  Also especially redundant because of Unicode!  Still, I'm sure somone can find use for it - perhaps to constrict filenames...
Mark


ru dot dy

Posting umlaute here resulted in a mess. Heres a version of the same function that works with preg_replace only:
<?php
 function getRewriteString($sString) {
    $string = strtolower(htmlentities($sString));
$string = preg_replace("/&(.)(uml);/", "$1e", $string);
$string = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml);/", "$1", $string);
    $string = preg_replace("/([^a-z0-9]+)/", "-", html_entity_decode($string));
    $string = trim($string, "-");
    return $string;
 }
?>


christophe

Latin1 (iso-8859-1) DONT define chars \x80-\x9f (128-159),
but Windows charset 1252 defines _some_ of them
-- like the infamous msoffice 'magic quotes' (\x92 146).
Dont use those invalid control chars in webpages,
but their html (unicode) entities. See ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
or http://www.microsoft.com/typography/unicode/1252.htm
PS: a '?' in the code means the win-cp1252 dont define the given char.
$badlatin1_cp1252_to_htmlent =
 array(
   '\x80'=>'&#x20AC;', '\x81'=>'?', '\x82'=>'&#x201A;', '\x83'=>'&#x0192;',
   '\x84'=>'&#x201E;', '\x85'=>'&#x2026;', '\x86'=>'&#x2020;', \x87'=>'&#x2021;',
   '\x88'=>'&#x02C6;', '\x89'=>'&#x2030;', '\x8A'=>'&#x0160;', '\x8B'=>'&#x2039;',
   '\x8C'=>'&#x0152;', '\x8D'=>'?', '\x8E'=>'&#x017D;', '\x8F'=>'?',
   '\x90'=>'?', '\x91'=>'&#x2018;', '\x92'=>'&#x2019;', '\x93'=>'&#x201C;',
   '\x94'=>'&#x201D;', '\x95'=>'&#x2022;', '\x96'=>'&#x2013;', '\x97'=>'&#x2014;',
   '\x98'=>'&#x02DC;', '\x99'=>'&#x2122;', '\x9A'=>'&#x0161;', '\x9B'=>'&#x203A;',
   '\x9C'=>'&#x0153;', '\x9D'=>'?', '\x9E'=>'&#x017E;', '\x9F'=>'&#x0178;'
 );
$str = strtr($str, $badlatin1_cp1252_to_htmlent);


rortiz_reyes

If you have trouble accessing a file which has an accented or tilde letter (á,é,í,ó,ú, or ñ) via Internet Explorer use the following translation table:
$trans = array("á" => "%E1", "é" => "%E9", "í" => "%ED", "ó" => "%F3","ú" => "%FA", "ñ" => "%D1",
"Á" => "%A1", "É" => "%A9", "Í" => "%AD", "Ó" => "%B3","Ú" => "%BA", "ñ" => "%F1");
To obtain the translation for other special characters not used in English (for example, ù), type a fictitious filename on the Netscape 7.1 address bar (including URL, for example www.url.com/ù.jpg) and press enter.  Netscape traslates the character while Explorer simply can't handle it.
Seems like another bug on Explorer 6.0...
Regards,
Ricardo Ortiz R.


11-dec-2004 01:20

If you are going to call strtr a lot, consider using str_replace instead, as it is much faster. I cut execution time in half just by doing this.
<?
// i.e. instead of:
$s=strtr($s,$replace_array);
// use:
foreach($replace_array as $key=>$value) $s=str_replace($key,$value,$s);
?>


from-php-net dot ticket

Hi, before I found strtr() function I quickly wrote own repleacement, if someone is interested,
// by http://www.raf256.com - Rafal Maj
function ConvCharset($from,$to,$s) {
 $l=strlen($s);
 $S=''; // out put
 for ($i=0; $i<$l; $i++) {
   $c=$s[$i]; // curr char
   $x=strpos($from, $c);
   if ($x!==FALSE) $c=$to[$x];
   $S.=$c;
 }
 return $S;
}


j

Here's a very useful function to translate Microsoft characters into Latin 15, so that people won't see any more square instead of characters in web pages .
function demicrosoftize($str) {
   return strtr($str,
"\x82\x83\x84\x85\x86\x87\x89\x8a" .
"\x8b\x8c\x8e\x91\x92\x93\x94\x95" .
"\x96\x97\x98\x99\x9a\x9b\x9c\x9e\x9f",
"'f\".**^\xa6<\xbc\xb4''" .
"\"\"---~ \xa8>\xbd\xb8\xbe");
}


info

Here's a nice function for parsing a string to something suitable for URL rewriting (mod_rewrite). It translates all accented characters to their non-accented equivalents and replaces all other non-alphanumeric character with dashes:
function getRewriteString($sString) {
$string = htmlentities(strtolower($string));
$string = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml);/", "$1", $string);
$string = preg_replace("/([^a-z0-9]+)/", "-", html_entity_decode($string));
$string = trim($string, "-");
return $string;
}


fernando "malk" piancastelli

Here's a function to replace linebreaks to html

tags. This was initially designed to receive a typed text by a form in a "insert new notice" page and put in a database, then a "notice" page could get the text preformatted with paragraph tags instead of linebreaks that won't appear on browser. The function also removes repeated linebreaks the user may have typed in the form.
function break_to_tags(&$text) {
      // find and remove repeated linebreaks
      $double_break = array("\r\n\r\n" => "\r\n");
      do {
             $text = strtr($text, $double_break);
             $position = strpos($text, "\r\n\r\n");
      } while ($position !== false);
      // find and replace remanescent linebreaks by

tags
      $change = array("\r\n" => "

");
      $text = strtr($text, $change);
}
[]'s
Fernando


ezbakhe yassin
Here you are a simple function to rotate a variable according to an array of possible values. You can make a strict comparison (===).
<?php
function rotateValue($string, $values, $strict = TRUE)
{
   if (!empty($string) AND is_array($values))
   {
       $valuesCount = count($values);
       for ($i = 0; $i < $valuesCount; $i++)
       {
           if ($strict ? ($string === $values[$i]) : ($string == $values[$i]))
           {
               return $values[($i + 1) % $valuesCount];
           }
       }
   }
   return FALSE;
}
?>
For example:
- rotateValue("A", array("A", "B", "C")) will return "B"
- rotateValue("C", array("A", "B", "C")) will return "A"


horak.jan

Here is a function to convert middle-european windows charset (cp1250) to the charset, that php script is written in:
<?php
function cp1250_to_utf2($text){
$dict  = array(chr(225) => 'á', chr(228) =>  'ä', chr(232) => 'č', chr(239) => 'ď',
chr(233) => 'é', chr(236) => 'ě', chr(237) => 'í', chr(229) => 'ĺ', chr(229) => 'ľ',
chr(242) => 'ň', chr(244) => 'ô', chr(243) => 'ó', chr(154) => 'š', chr(248) => 'ř',
chr(250) => 'ú', chr(249) => 'ů', chr(157) => 'ť', chr(253) => 'ý', chr(158) => 'ž',
chr(193) => 'Á', chr(196) => 'Ä', chr(200) => 'Č', chr(207) => 'Ď', chr(201) => 'É',
chr(204) => 'Ě', chr(205) => 'Í', chr(197) => 'Ĺ', chr(188) => 'Ľ', chr(210) => 'Ň',
chr(212) => 'Ô', chr(211) => 'Ó', chr(138) => 'Š', chr(216) => 'Ř', chr(218) => 'Ú',
chr(217) => 'Ů', chr(141) => 'Ť', chr(221) => 'Ý', chr(142) => 'Ž',
chr(150) => '-');
return strtr($text, $dict);
}
?>


stian

elonen forgot the ø character (\xf8)
A (more) complete accent remover:
$txt = strtr($txt,
"\xe1\xc1\xe0\xc0\xe2\xc2\xe4\xc4\xe3\xc3\xe5\xc5".
"\xaa\xe7\xc7\xe9\xc9\xe8\xc8\xea\xca\xeb\xcb\xed".
"\xcd\xec\xcc\xee\xce\xef\xcf\xf1\xd1\xf3\xd3\xf2".
"\xd2\xf4\xd4\xf6\xd6\xf5\xd5\x8\xd8\xba\xf0\xfa\xda".
"\xf9\xd9\xfb\xdb\xfc\xdc\xfd\xdd\xff\xe6\xc6\xdf\xf8",
"aAaAaAaAaAaAacCeEeEeEeEiIiIiIiInNo".
"OoOoOoOoOoOoouUuUuUuUyYyaAso");


symlink23-remove-my-spleen

As noted in the str_rot13 docs, some servers don't provide the str_rot13() function. However, the presence of strtr makes it easy to build your own facsimile thereof:
if (!function_exists('str_rot13')) {
   function str_rot13($str) {
       $from = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
       $to   = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
       return strtr($str, $from, $to);
   }
}
This is suitable for very light "encryption" such as hiding email addressess from spambots (then unscrambling them in a mail class, for example).
$mail_to=str_rot13("$mail_to");


patrick

As Daijoubu suggested use str_replace instead of this function for large arrays/subjects. I just tried it with a array of 60 elements, a string with 8KB length, and the execution time of str_replace was faster at factor 20!
Patrick


erik

As an alternative to the not-yet-existing function stritr mentioned in the first note above You can easily do this:
strtr("abc","ABCabc","xyzxyz")
or more general:
strtr("abc",
strtoupper($fromchars).strtolower($fromchars),
$tochars.$tochars);
Just a thought.


-sven www.bitcetera.com

And while we're at it, yet another transcriber (the code formerly known as accent remover). It does accents and umlauts, but also ligatures and runes known to ISO-8859-1. The translation strings must be on one line without any whitespaces in it. They are rendered hardwrapped here because this documentation doesn't allow long lines in notes.
function transcribe($string) {
   $string = strtr($string,
      "\xA1\xAA\xBA\xBF\xC0\xC1\xC2\xC3\xC5\xC7
       \xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1
       \xD2\xD3\xD4\xD5\xD8\xD9\xDA\xDB\xDD\xE0
       \xE1\xE2\xE3\xE5\xE7\xE8\xE9\xEA\xEB\xEC
       \xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF8
       \xF9\xFA\xFB\xFD\xFF",
       "!ao?AAAAAC
       EEEEIIIIDN
       OOOOOUUUYa
       aaaaceeeei
       iiidnooooo
       uuuyy");  
   $string = strtr($string, array("\xC4"=>"Ae", "\xC6"=>"AE", "\xD6"=>"Oe", "\xDC"=>"Ue", "\xDE"=>"TH", "\xDF"=>"ss", "\xE4"=>"ae", "\xE6"=>"ae", "\xF6"=>"oe", "\xFC"=>"ue", "\xFE"=>"th"));
   return($string);
}
(Funky: ISO-8859-1 does not cover the french "oe" ligature.)


joeldegan

After battling with strtr trying to strip out MS word formatting from things pasted into forms I ended up coming up with this..
it strips ALL non-standard ascii characters, preserving html codes and such, but gets rid of all the characters that refuse to show in firefox.
If you look at this page in firefox you will see a ton of "question mark" characters and so it is not possible to copy and paste those to remove them from strings..  (this fixes that issue nicely, though I admit it could be done a bit better)
<?
function fixoutput($str){
$good[] = 9;  #tab
$good[] = 10; #nl
$good[] = 13; #cr
for($a=32;$a<127;$a++){
$good[] = $a;
}
$len = strlen($str);
for($b=0;$b < $len+1; $b++){
if(in_array(ord($str[$b]), $good)){
$newstr .= $str[$b];
}//fi
}//rof
return $newstr;
}
?>


derernst

Additional to ru dot dy at gmx dot net's code:
1. You might want to convert &szlig; to ss additionally
2. There could be impacts with other languages that use the umlaut characters: ä -> ae conversion for northern european languages such as Swedish, or ü -> ue conversion for Turkish. It would be useful if speakers of these and other languages could post their languages' correct conversions here.


sanate

// Translates  iso-8859-2  into  UTF-8
function latin2_to_utf8($text) {
$trans = array(
chr(225)=>'á', chr(193)=>'Á', chr(232)=>'è', chr(200)=>'È', chr(239)=>'ï', chr(207)=>'Ï',
chr(233)=>'é', chr(201)=>'É', chr(236)=>'ì', chr(204)=>'Ì', chr(237)=>'í', chr(205)=>'Í',
chr(181)=>'¾', chr(165)=>'¼', chr(242)=>'ò', chr(210)=>'Ò', chr(243)=>'ó', chr(211)=>'Ó',
chr(248)=>'ø', chr(216)=>'Ø', chr(185)=>'š', chr(169)=>'Š', chr(187)=>'', chr(171)=>'',
chr(250)=>'ú', chr(218)=>'Ú', chr(249)=>'ù', chr(217)=>'Ù', chr(253)=>'ý', chr(221)=>'Ý',
chr(190)=>'ž', chr(174)=>'Ž', chr(180)=>"'"
);
 return  strtr($text, $trans);
}
// enjoy! :)


martin dot pelikan

// if you are upset with windows' ^M characters at the end of the line,
// these two lines are for you:
$trans = array("\x0D" => "");
$text = strtr($orig_text,$trans);
// note that ctrl+M (in vim known as ^M) is hexadecimally 0x0D


sanate

// Hello to all Czech and Slovak people!
// I hope this function can be useful and easier to find here,
// than at the original source (and opposite direction). :
//                  http://www.kosek.cz/clanky/tipy/qa07.html
// s pozdravem   Filip Rydlo  z Pohodasoftware.Cz
function latin2_to_win1250($text) {  // chce text v iso-88592
$text = StrTr($text, "\xA\xAB\xAE\xB\xBB\xBE",
                           "\x8A\x8D\x8E\x9A\x9D\x9E");
return $text;
}


ajitsingh4u

/**
* Replaces special characters with single quote,double quote and comma for charset iso-8859-1
*
* replaceSpecialChars()
* @param string $str
* @return string
*/
function replaceSpecialChars($str)
{
//`(96) ’(130) „(132) ‘(145) ’(146) “(147) ”(148) ´(180)   // equivalent ascii values of these characters.
$str = strtr($str, "`’„‘’´", "'','''");
$str = strtr($str, '“”', '""');
return $str;
}


tomhmambo

<?
// Windows-1250 to ASCII
// This function replace all Windows-1250 accent characters with
// thier non-accent ekvivalents. Useful for Czech and Slovak languages.
function win2ascii($str) {
$str = StrTr($str,
"\xE1\xE8\xEF\xEC\xE9\xED\xF2",
"\x61\x63\x64\x65\x65\x69\x6E");

$str = StrTr($str,
"\xF3\xF8\x9A\x9D\xF9\xFA\xFD\x9E\xF4\xBC\xBE",
"\x6F\x72\x73\x74\x75\x75\x79\x7A\x6F\x4C\x6C");

$str = StrTr($str,
"\xC1\xC8\xCF\xCC\xC9\xCD\xC2\xD3\xD8",
"\x41\x43\x44\x45\x45\x49\x4E\x4F\x52");

$str = StrTr($str,
"\x8A\x8D\xDA\xDD\x8E\xD2\xD9\xEF\xCF",
"\x53\x54\x55\x59\x5A\x4E\x55\x64\x44");
return $str;
}
?>


bisqwit

#!/bin/sh
# This shell script generates a strtr() call
# to translate from a character set to another.
# Requires: gnu recode, perl, php commandline binary
#
# Usage:
#  Set set1 and set2 to whatever you prefer
#  (multibyte character sets are not supported)
#  and run the script. The script outputs
#  a strtr() php code for you to use.
#
# Example is set to generate a
# cp437..latin9 conversion code.
#
set1=cp437
set2=iso-8859-15
result="`echo '<? for($c=32;$c<256;$c++)'\
             'echo chr($c);'\            
        |php -q|recode -f $set1..$set2`"
echo "// This php function call converts \$string in $set1 to $set2";
cat <<EOF  | php -q
<?php
\$set1='`echo -n "$result"\
  |perl -pe "s/([\\\\\'])/\\\\\\\\\\$1/g"`';
\$set2='`echo -n "$result"|recode -f $set2..$set1\
  |perl -pe "s/([\\\\\'])/\\\\\\\\\\$1/g"`';
\$erase=array();
\$l=strlen(\$set1);
for(\$c=0;\$c<\$l;++\$c)
 if(\$set1[\$c]==\$set2[\$c])\$erase[\$set1[\$c]]='';
if(count(\$erase))
{
 \$set1=strtr(\$set1,\$erase);
 \$set2=strtr(\$set2,\$erase);
}
if(!strlen(\$set1))echo 'IRREVERSIBLE';else
echo "strtr(\\\$string,\n  '",
    ereg_replace('([\\\\\\'])', '\\\\\\1', \$set2),
    "',\n  '",
    ereg_replace('([\\\\\\'])', '\\\\\\1', \$set1),
    "');";
EOF


Change Language


Follow Navioo On Twitter
addcslashes
addslashes
bin2hex
chop
chr
chunk_split
convert_cyr_string
convert_uudecode
convert_uuencode
count_chars
crc32
crypt
echo
explode
fprintf
get_html_translation_table
hebrev
hebrevc
html_entity_decode
htmlentities
htmlspecialchars_decode
htmlspecialchars
implode
join
levenshtein
localeconv
ltrim
md5_file
md5
metaphone
money_format
nl_langinfo
nl2br
number_format
ord
parse_str
print
printf
quoted_printable_decode
quotemeta
rtrim
setlocale
sha1_file
sha1
similar_text
soundex
sprintf
sscanf
str_getcsv
str_ireplace
str_pad
str_repeat
str_replace
str_rot13
str_shuffle
str_split
str_word_count
strcasecmp
strchr
strcmp
strcoll
strcspn
strip_tags
stripcslashes
stripos
stripslashes
stristr
strlen
strnatcasecmp
strnatcmp
strncasecmp
strncmp
strpbrk
strpos
strrchr
strrev
strripos
strrpos
strspn
strstr
strtok
strtolower
strtoupper
strtr
substr_compare
substr_count
substr_replace
substr
trim
ucfirst
ucwords
vfprintf
vprintf
vsprintf
wordwrap
eXTReMe Tracker