Description
string strtr (string str, string from, string to)
strtr() travaille sur str,
remplacant chaque occurence de chaque caractère de la chaîne
from correspondant à la chaîne
to et retourne le résultat.
Si from et to sont de longueur
différentes, les caractères en trop sont ignorés.
Exemple 1. Exemple avec strtr()
<?php
$addr = strtr($addr, "AAÖ", "aao");
?>
|
|
strtr() peut aussi être appelée avec deux
arguments. Dans ce cas, elle se comporte différemment :
from doit être un tableau associatif contenant
des paires de chaînes, qui seront remplacées dans la chaîne
source. strtr() recherchera toujours la chaîne la plus
longue, et la remplacera en premier. Elle ne remplacera jamais
une portion de chaîne qu'elle à déjà remplacé.
Exemples:
Cete exemple affichera : "salut à tous, j'ai dit bonjour",
Note :
Travailler avec deux arguments a été ajouté dans PHP 4.0.
Voir aussi ereg_replace().
| User Contributed Notes: strtr |
clambert@gamespy.com
17-Aug-2000 05:40 |
This function is case sensitive. Therefore, strtr("abc","ABC","xyz") would return abc.
Perhaps we'll see an stritr in the future, but otherwise you'll have to:
1) call it with only two paramaters and use the array feature (PHP 4)
2) add on the extra characters to the end of both $to and $from
3) use eregi_replace
|
|
herve@milk.fr
11-Oct-2000 07:21 |
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"," ");
?>
|
|
bvr@xs4all.nl
12-Jan-2001 06:38 |
for translating html entities to text use this:
$trans = array_flip(get_html_translation_table(HTML_ENTITIES));
$trans["&"] = "&";
$str = strtr($str,$trans);
note that when you reverse the translation table with array_flip()
some characters aren't translated correctly (like & for example).
to check the translation table use:
print_r($trans);
bvr.
|
|