超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<strtouppersubstr_compare>
Last updated: Fri, 22 Jun 2007

strtr

(PHP 3, PHP 4 , PHP 5)

strtr -- Translate certain characters

Description

string strtr ( string str, string from, string to)

string strtr ( string str, array replace_pairs)

This function returns a copy of str, translating all occurrences of each character in from to the corresponding character in to.

If from and to are different lengths, the extra characters in the longer of the two are ignored.

例子 1. strtr() example

<?php
$addr
= strtr($addr, "溴觫, "aao");
?>

strtr() may be called with only two arguments. If called with two arguments it behaves in a new way: from then has to be an array that contains string -> string pairs that will be replaced in the source string. strtr() will always look for the longest possible match first and will *NOT* try to replace stuff that it has already worked on.

例子 2. 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

注: This optional to and from parameters were added in PHP 4.0.0

See also ereg_replace().




add a note add a note User Contributed Notes
strtr
herve at milk dot fr
11-Oct-2000 10: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","   ");
?>

<strtouppersubstr_compare>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22