字符串函数
在线手册:中文 英文
PHP手册

strtr

(PHP 4, PHP 5)

strtr转换指定字符

说明

string strtr ( string $str , string $from , string $to )
string strtr ( string $str , array $replace_pairs )

该函数返回 str 的一个副本,并将在 from 中指定的字符转换为 to 中相应的字符。

如果 fromto 长度不相等,那么多余的字符部分将被忽略。

参数

str

待转换的字符串

from

字符串中与将要被转换的目的字符 to 相对应的源字符。

to

字符串中与将要被转换的字符 from 相对应的目的字符。

replace_pairs

参数 replace_pairs 可以用来取代 tofrom 参数,因为它是以 array('from' => 'to', ...) 格式出现的数组。

返回值

返回转换后的字符串

如果 replace_pairs 中包含一个空字符串"")键,那么将返回 FALSE

范例

Example #1 strtr() 范例

<?php
$addr 
strtr($addr"???""aao");
?>

可以使用两个参数调用 strtr()。如果被以两个参数调用,它将以不同的方式运行:from 必须是一组待转换的字符串对。strtr() 总是首先尽可能尝试最长的匹配,并且它不会处理已经被转换过的部分。

Example #2 使用两个参数的 strtr() 范例

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

以上例程会输出:

hello all, I said hi

参见


字符串函数
在线手册:中文 英文
PHP手册
PHP手册 - N: 转换指定字符

用户评论:

antimoz at gmail dot com (09-Feb-2012 11:08)

Here is my array for char normalization:
        $normalizeChars = array(
            '&Aacute;'=>'A', '&Agrave;'=>'A', '&Acirc;'=>'A', '&Atilde;'=>'A', '&Aring;'=>'A', '&Auml;'=>'A', '&AElig;'=>'AE', '&Ccedil;'=>'C',
            '&Eacute;'=>'E', '&Egrave;'=>'E', '&Ecirc;'=>'E', '&Euml;'=>'E', '&Iacute;'=>'I', '&Igrave;'=>'I', '&Icirc;'=>'I', '&Iuml;'=>'I', '&ETH;'=>'Eth',
            '&Ntilde;'=>'N', '&Oacute;'=>'O', '&Ograve;'=>'O', '&Ocirc;'=>'O', '&Otilde;'=>'O', '&Ouml;'=>'O', '&Oslash;'=>'O',
            '&Uacute;'=>'U', '&Ugrave;'=>'U', '&Ucirc;'=>'U', '&Uuml;'=>'U', '&Yacute;'=>'Y',
   
            '&aacute;'=>'a', '&agrave;'=>'a', '&acirc;'=>'a', '&atilde;'=>'a', '&aring;'=>'a', '&auml;'=>'a', '&aelig;'=>'ae', '&ccedil;'=>'c',
            '&eacute;'=>'e', '&egrave;'=>'e', '&ecirc;'=>'e', '&euml;'=>'e', '&iacute;'=>'i', '&igrave;'=>'i', '&icirc;'=>'i', '&iuml;'=>'i', '&eth;'=>'eth',
            '&ntilde;'=>'n', '&oacute;'=>'o', '&ograve;'=>'o', '&ocirc;'=>'o', '&otilde;'=>'o', '&ouml;'=>'o', '&oslash;'=>'o',
            '&uacute;'=>'u', '&ugrave;'=>'u', '&ucirc;'=>'u', '&uuml;'=>'u', '&yacute;'=>'y',
           
            '&szlig;'=>'sz', '&thorn;'=>'thorn', '&yuml;'=>'y'
        );

Sam (02-Feb-2012 06:33)

<?php // Case Insensitive strtr
function stritr($string, $one, $two=null) {
    if (
is_string($one)) {
        return
strtr($string, strtoupper($one) . strtolower($one), "$two$two");
    } else if (
is_array($one)) {
       
$strReturn = $string
       
foreach ($one as $key => $val) {
           
$strReturn = preg_replace("'$key'i", $val, $strReturn);
        }
        return
$strReturn;
    }
    return
$string;
}
?>

Michael Schuijff (24-Oct-2011 06:02)

I found that this approach is often faster than strtr() and won't change the same thing in your string twice (as opposed to str_replace(), which will overwrite things in the order of the array you feed it with):

<?php
function replace ($text, $replace) {
   
$keys = array_keys($replace);
   
$length = array_combine($keys, array_map('strlen', $keys));
   
arsort($length);
   
   
$array[] = $text;
   
$count = 1;
   
reset($length);
    while (
$key = key($length)) {
        if (
strpos($text, $key) !== false) {
            for (
$i = 0; $i < $count; $i += 2) {
                if ((
$pos = strpos($array[$i], $key)) === false) continue;
               
array_splice($array, $i, 1, array(substr($array[$i], 0, $pos), $replace[$key], substr($array[$i], $pos + strlen($key))));
               
$count += 2;
            }
        }
       
next($length);
    }
    return
implode($array);
}
?>

Chris (04-Feb-2011 08:49)

Hope this is useful when you need to see ASCII control characters:
<?php
$xlate
= array(chr(0) => '^@/NUL/null', chr(1) => '^A/SOH/start of heading', chr(2) => '^B/STX/start of text', chr(3) => '^C/ETX/end of text', chr(4) => '^D/EOT/end of transmisssion', chr(5) => '^E/ENQ/enquiry', chr(6) => '^F/ACK/acknowledge', chr(7) => '^G/BEL/bell', chr(8) => '^H/BS/backspace', chr(9) => '^I/TAB/horizontal tab', chr(10) => '^J/LF/NL/line feed/new line', chr(11) => '^K/VT/vertical tab', chr(12) => '^L/FF/NP/form feed/new page/', chr(13) => '^M/CR/carrige return', chr(14) => '^N/SO/shift out', chr(15) => '^O/SI/shift in', chr(16) => '^P/DLE/data link escape', chr(17) => '^Q/DC1/device control 1', chr(18) => '^R/DC2/device control 2', chr(19) => '^S/DC3/device control 3', chr(20) => '^T/DC4/device control 4', chr(21) => '^U/NAK/negative acknowledge', chr(22) => '^V/SYN/synchronous idle', chr(23) => '^W/ETB/end of transmission block', chr(24) => '^X/CAN/cancel', chr(25) => '^Y/EM/end of medium', chr(26) => '^Z/SUB/substiute', chr(27) => '^[/ESC/escape', chr(28) => '^\/FS/file separator', chr(29) => '^]/GS/group separator', chr(30) => '^^/RS/record separator', chr(31) => '^_/US/unit separator', chr(32) => 'Space');

$x = 0;
$pad = strlen(strlen($str));
while(isset(
$str[$x]))
   echo
'character ', str_pad($x+1, $pad), ' = ', strtr($str[$x], $xlate), ' (ascii ', ord($str[$x++]), ')';
?>

sales at mk2solutions dot com (29-Jun-2010 07:59)

This is what we use for prepping data that is going to be used for shortURLs or just needs to be completely cleaned
<?php

$GLOBALS
['normalizeChars'] = array(
   
'?'=>'S', '?'=>'s', '?'=>'Dj','?'=>'Z', '?'=>'z', '?'=>'A', '?'=>'A', '?'=>'A', '?'=>'A', '?'=>'A',
   
'?'=>'A', '?'=>'A', '?'=>'C', '?'=>'E', '?'=>'E', '?'=>'E', '?'=>'E', '?'=>'I', '?'=>'I', '?'=>'I',
   
'?'=>'I', '?'=>'N', '?'=>'O', '?'=>'O', '?'=>'O', '?'=>'O', '?'=>'O', '?'=>'O', '?'=>'U', '?'=>'U',
   
'?'=>'U', '?'=>'U', '?'=>'Y', '?'=>'B', '?'=>'Ss','à'=>'a', 'á'=>'a', '?'=>'a', '?'=>'a', '?'=>'a',
   
'?'=>'a', '?'=>'a', '?'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', '?'=>'e', 'ì'=>'i', 'í'=>'i', '?'=>'i',
   
'?'=>'i', '?'=>'o', '?'=>'n', 'ò'=>'o', 'ó'=>'o', '?'=>'o', '?'=>'o', '?'=>'o', '?'=>'o', 'ù'=>'u',
   
'ú'=>'u', '?'=>'u', '?'=>'y', '?'=>'y', '?'=>'b', '?'=>'y', '?'=>'f'
);

function
cleanForShortURL($toClean) {
   
$toClean     =     str_replace('&', '-and-', $toClean);
   
$toClean     =    trim(preg_replace('/[^\w\d_ -]/si', '', $toClean));//remove all illegal chars
   
$toClean     =     str_replace(' ', '-', $toClean);
   
$toClean     =     str_replace('--', '-', $toClean);
   
    return
strtr($toClean, $GLOBALS['normalizeChars']);
}

This process cleans up any special characters and also coverts strings to a readable safe URL format.
?>

elloromtz at gmail dot com (20-Apr-2010 01:08)

If you supply 3 arguments and the 2nd is an array, strtr will search the "A" from "Array" (because you're treating it as a scalar string) and replace it with the 3rd argument:

strtr('Analogy', array('x'=>'y'),  '_'); //'_nalogy'

so in reality the above code has the same affect as:

strtr('Analogy', 'A' , '_');

Anonymous (25-Nov-2009 08:09)

Here's a one-liner to strip out non-standard ascii characters, inspired by joeldegan AT yahoo's post below.

<?php
  $new
= preg_replace("/[^\x9\xA\xD\x20-\x7F]/", "", $old);
?>

nvyktor (09-Sep-2009 03:02)

Hi all,

as u probably know the is some truoble with the (for example) hungarian special characters. If I used the htmlentities() function, the simple chars had benn converted to the basic format, for example: & aacute;. However this was very simple, some cases it needs more transformation.

As I would like to use the correct caracters even in php, html, js, and more, a wrote this short code to solve this issue:

<?php
function charcode ($text) {
   
$text = htmlentities($text); //to convert the simple spec chars
   
$search = array("& otilde;","&O tilde;","& ucirc;","&U circ;");
   
$replace = array("& #337;","& #336;","& #369;","& #368;");
   
$text = str_replace($search, $replace, $text);
    return
$text;
}
?>

Now I am able to display any spec chars in any browser with any character encoding set.

Hope U will find this helpful.

Vyktor

allixsenos at gmail dot com (16-May-2009 04:55)

fixed "normaliza" functions written below to include Slavic Latin characters... also, it doesn't return lowercase any more (you can easily get that by applying strtolower yourself)...

also, renamed to normalize()

<?php

function normalize ($string) {
   
$table = array(
       
'?'=>'S', '?'=>'s', '?'=>'Dj', '?'=>'dj', '?'=>'Z', '?'=>'z', '?'=>'C', '?'=>'c', '?'=>'C', '?'=>'c',
       
'?'=>'A', '?'=>'A', '?'=>'A', '?'=>'A', '?'=>'A', '?'=>'A', '?'=>'A', '?'=>'C', '?'=>'E', '?'=>'E',
       
'?'=>'E', '?'=>'E', '?'=>'I', '?'=>'I', '?'=>'I', '?'=>'I', '?'=>'N', '?'=>'O', '?'=>'O', '?'=>'O',
       
'?'=>'O', '?'=>'O', '?'=>'O', '?'=>'U', '?'=>'U', '?'=>'U', '?'=>'U', '?'=>'Y', '?'=>'B', '?'=>'Ss',
       
'à'=>'a', 'á'=>'a', '?'=>'a', '?'=>'a', '?'=>'a', '?'=>'a', '?'=>'a', '?'=>'c', 'è'=>'e', 'é'=>'e',
       
'ê'=>'e', '?'=>'e', 'ì'=>'i', 'í'=>'i', '?'=>'i', '?'=>'i', '?'=>'o', '?'=>'n', 'ò'=>'o', 'ó'=>'o',
       
'?'=>'o', '?'=>'o', '?'=>'o', '?'=>'o', 'ù'=>'u', 'ú'=>'u', '?'=>'u', '?'=>'y', '?'=>'y', '?'=>'b',
       
'?'=>'y', '?'=>'R', '?'=>'r',
    );
   
    return
strtr($string, $table);
}

?>

Sidney Ricardo (05-Sep-2008 07:54)

This work fine to me:

<?php
function normaliza ($string){
   
$a = '??????????????????????????????
?àá??????èéê?ìí????òó????ùú???????'
;
   
$b = 'aaaaaaaceeeeiiiidnoooooouuuuy
bsaaaaaaaceeeeiiiidnoooooouuuyybyRr'
;
   
$string = utf8_decode($string);    
   
$string = strtr($string, utf8_decode($a), $b);
   
$string = strtolower($string);
    return
utf8_encode($string);
}
?>

jorge at seisbits dot com (11-Jul-2008 08:04)

If you try to make a strtr of not usual charafters when you are in a utf8 enviroment, you can do that:

function normaliza ($string){
    $string = utf8_decode($string);
    $string = strtr($string, utf8_decode(" ??????"), "-AEIOU");
    $string = strtolower($string);
    return $string;
}

dot dot dot dot dot alexander at gmail dot com (26-Mar-2008 12:09)

OK, I debugged the function (had some errors)
Here it is:

if(!function_exists("stritr")){
    function stritr($string, $one = NULL, $two = NULL){
/*
stritr - case insensitive version of strtr
Author: Alexander Peev
Posted in PHP.NET
*/
        if(  is_string( $one )  ){
            $two = strval( $two );
            $one = substr(  $one, 0, min( strlen($one), strlen($two) )  );
            $two = substr(  $two, 0, min( strlen($one), strlen($two) )  );
            $product = strtr(  $string, ( strtoupper($one) . strtolower($one) ), ( $two . $two )  );
            return $product;
        }
        else if(  is_array( $one )  ){
            $pos1 = 0;
            $product = $string;
            while(  count( $one ) > 0  ){
                $positions = array();
                foreach(  $one as $from => $to  ){
                    if(   (  $pos2 = stripos( $product, $from, $pos1 )  ) === FALSE   ){
                        unset(  $one[ $from ]  );
                    }
                    else{
                        $positions[ $from ] = $pos2;
                    }
                }
                if(  count( $one ) <= 0  )break;
                $winner = min( $positions );
                $key = array_search(  $winner, $positions  );
                $product = (   substr(  $product, 0, $winner  ) . $one[$key] . substr(  $product, ( $winner + strlen($key) )  )   );
                $pos1 = (  $winner + strlen( $one[$key] )  );
            }
            return $product;
        }
        else{
            return $string;
        }
    }/* endfunction stritr */
}/* endfunction exists stritr */

dot dot dot dot dot alexander at gmail dot com (25-Mar-2008 06:44)

Here is the stritr I always needed... I wrote it in 15 minutes... But only after the idea struck me. Hope you find it helpful, and enjoy...
<?php
if(!function_exists("stritr")){
    function
stritr($string, $one = NULL, $two = NULL){
/*
stritr - case insensitive version of strtr
Author: Alexander Peev
Posted in PHP.NET
*/
       
if(  is_string( $one )  ){
           
$two = strval( $two );
           
$one = substr$one, 0, min( strlen($one), strlen($two) )  );
           
$two = substr$two, 0, min( strlen($one), strlen($two) )  );
           
$product = strtr$string, ( strtoupper($one) . strtolower($one) ), ( $two . $two )  );
            return
$product;
        }
        else if( 
is_array( $one )  ){
           
$pos1 = 0;
           
$product = $string;
            while( 
count( $one ) > ){
               
$positions = array();
                foreach( 
$one as $from => $to  ){
                    if(   ( 
$pos2 = stripos( $product, $from, $pos1 )  ) === FALSE   ){
                        unset( 
$one[ $from ]  );
                    }
                    else{
                       
$positions[ $from ] = $pos2;
                    }
                }
               
$winner = min( $positions );
               
$key = array_search$winner, $positions  );
               
$product = (   substr$product, 0, $winner  ) . $positions[$key] . substr$product, ( $winner + strlen($key) )  )   );
               
$pos1 = (  $winner + strlen( $positions[$key] )  );
            }
            return
$product;
        }
        else{
            return
$string;
        }
    }
/* endfunction stritr */
}/* endfunction exists stritr */
?>

Jean-Marc Libs (26-Feb-2008 05:49)

A couple of people have suggested examples of use of strstr() in order to do conversions from one charset to the other.

I would like to point out that this is the purpose of iconv().

troelskn at gmail dot com (23-Jan-2008 11:39)

Here's another transcribe function. This one converts cp1252 (aka. Windows-1252) into iso-8859-1 (aka. latin1, the default PHP charset). It only transcribes the few exotic characters, which are unique to cp1252.

function transcribe_cp1252_to_latin1($cp1252) {
  return strtr(
    $cp1252,
    array(
      "\x80" => "e",  "\x81" => " ",    "\x82" => "'", "\x83" => 'f',
      "\x84" => '"',  "\x85" => "...",  "\x86" => "+", "\x87" => "#",
      "\x88" => "^",  "\x89" => "0/00", "\x8A" => "S", "\x8B" => "<",
      "\x8C" => "OE", "\x8D" => " ",    "\x8E" => "Z", "\x8F" => " ",
      "\x90" => " ",  "\x91" => "`",    "\x92" => "'", "\x93" => '"',
      "\x94" => '"',  "\x95" => "*",    "\x96" => "-", "\x97" => "--",
      "\x98" => "~",  "\x99" => "(TM)", "\x9A" => "s", "\x9B" => ">",
      "\x9C" => "oe", "\x9D" => " ",    "\x9E" => "z", "\x9F" => "Y"));

ajitsingh4u at gmail dot com (06-Aug-2007 03:36)

/**
* 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;
}

peter dot goodman at gmail dot com (28-Jun-2007 04:17)

To the previous comment: great function, one character mapping it is missing is though is:

chr(226) => '?'

horak.jan AT centrum.cz (22-May-2007 03:11)

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);
    }
?>

joeldegan AT yahoo (07-Apr-2006 04:49)

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;
}
?>

martin[dot]pelikan[at]gmail[dot]com (29-Dec-2005 03:20)

// 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

tomhmambo at seznam dot cz (20-Dec-2005 10:54)

<?
// 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;
}
?>

Ezbakhe Yassin <yassin88 at gmail dot com> (31-Aug-2005 11:55)

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"

ru dot dy at gmx dot net (11-Jul-2005 12:20)

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;
  }
?>

-sven (www.bitcetera.com) (21-Apr-2005 07:48)

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.)

info at oscaralexander dot com (13-Apr-2005 08:32)

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;
}

Stian (02-Mar-2005 09:58)

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");

elonen at iki dot fi (25-Feb-2005 08:24)

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");

patrick at p-roocks dot de (06-Feb-2005 10:31)

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

Daijoubu (12-Jan-2005 11:19)

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

(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);
?>

oliver at modix dot de (22-Oct-2004 09:08)

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);
}

?>

ktogias at math dot upatras dot gr (23-Sep-2004 11:32)

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."'";

from-php-net dot ticket at raf256 dot com (05-Jun-2004 12:59)

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;
}

volkris at tamu dot edu (19-Mar-2004 02:25)

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

stewey at ambitious dot ca (05-Mar-2004 02:11)

This version of macRomanToIso (originally posted by: marcus at synchromedia dot co dot uk) offers a couple of improvements. First, it removes the extra slashes '\' that broke the original function. Second, it adds four quote characters not supported in ISO 8859-1. These are the left double quote, right double quote, left single quote and right single quote.

Be sure to remove the line breaks from the two strings going into strtr or this function will not work properly.

Be careful what text you apply this to. If you apply it to ISO 8859-1 encoded text it will likely wreak havoc. I'll save you some trouble with this bit of advice: don't bother trying to detect what charset a certain text file is using, it can't be done reliably. Instead, consider making assumptions based upon the HTTP_USER_AGENT, or prompting the user to specify the character encoding used (perhaps both).

<?php

/**
 * Converts MAC OS ROMAN encoded strings to the ISO 8859-1 charset.
 *
 * @param    string    the string to convert.
 * @return    string    the converted string.
 */
function macRomanToIso($string)
{
    return
strtr($string,
"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b
\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97
\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa1\xa4\xa6\xa7
\xa8\xab\xac\xae\xaf\xb4\xbb\xbc\xbe\xbf\xc0\xc1
\xc2\xc7\xc8\xca\xcb\xcc\xd6\xd8\xdb\xe1\xe5\xe6
\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf1\xf2\xf3
\xf4\xf8\xfc\xd2\xd3\xd4\xd5"
,
"\xc4\xc5\xc7\xc9\xd1\xd6\xdc\xe1\xe0\xe2\xe4\xe3
\xe5\xe7\xe9\xe8\xea\xeb\xed\xec\xee\xef\xf1\xf3
\xf2\xf4\xf6\xf5\xfa\xf9\xfb\xfc\xb0\xa7\xb6\xdf\xae
\xb4\xa8\xc6\xd8\xa5\xaa\xba\xe6\xf8\xbf\xa1\xac
\xab\xbb\xa0\xc0\xc3\xf7\xff\xa4\xb7\xc2\xca\xc1
\xcb\xc8\xcd\xce\xcf\xcc\xd3\xd4\xd2\xda\xdb\xd9
\xaf\xb8\x22\x22\x27\x27"
);
}

?>

christophe at publicityweb dot com (26-Feb-2004 08:04)

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 at hotmail dot com (27-Jan-2004 05:15)

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.

j at pureftpd dot org (30-Nov-2003 04:24)

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");
}

Fernando "Malk" Piancastelli (29-Oct-2003 09:31)

Here's a function to replace linebreaks to html <p> 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 <p> tags

       $change = array("\r\n" => "<p>");
       $text = strtr($text, $change);
}

[]'s
Fernando

Sanate at seznam dot cz (17-Jul-2003 01:51)

// 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;
}

mykel at has dot it (06-Feb-2003 02:08)

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 */
?>

hotmail - marksteward (27-Nov-2002 03:39)

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

m dot frank at beam dot ag (22-Nov-2002 01:12)

to get the ascii equivalent of unicode characters simply use the
 
utf8_decode() function

marco dot colombo at nexor dot it (12-Nov-2002 02:20)

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

bisqwit at iki dot fi (10-Aug-2002 06:18)

#!/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),
     "'
,\'",
     ereg_replace('
([\\\\\\'])', '\\\\\\1', \$set1),
    
"');";
EOF

gabi at unica dot edu (17-Jul-2002 01:32)

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.

symlink23-remove-my-spleen at yahoo dot com (19-Apr-2002 05:33)

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");

erik at eldata dot se (23-Nov-2001 05:08)

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.