/**
* 1. Strtr converting a specified character
*
* String Strtr (String $str, String $from, String $to)
* String Strtr (string $str, array $replace _pairs)
*
* This function returns a copy of Str and converts the character specified in the From to the corresponding character in to.
* If the from and to lengths are not equal, the extra character parts will be ignored.
*/
$str = ' http://flyer0126.iteye.com/';
Echo strtr ($str, ' IT ', ' Java ');
OUTPUT:HTTP://FLYER0126.ITEYE.COM/STRTR Case Sensitive
If the from and to lengths are not equal, the extra character parts are ignored
Echo strtr ($str, ' It ', ' Java ');
output:haap://flyer0126.jaeye.com/
Iteye---Jaeye It's just for JA.
HTTP---HAAP word-by-character replacement of the corresponding position, which does not fit our purpose
Echo strtr ($str, ' it ', ');
output:http://flyer0126.iteye.com/No replacement
Echo strtr ($str, ' it ', ');
output:http://flyer0126. teye.com/can be replaced
/**
* The From->to way of function STRTR is summarized as follows:
* 1. Case sensitive;
* 2. When the form and to length are unequal, the extra characters will be ignored, can not be changed less, and can not be changed more;
* 3. Replace the corresponding position by character;
* 4. cannot be replaced with empty and can be replaced by spaces.
*/
In comparison, the latter approach is more appropriate
$replace _pairs = Array (
'/http '/' = ',
' It ' = ' java '
);
Echo Strtr ($str, $replace _pairs);
output:flyer0126.javaeye.com/replacement success, in line with the original replacement
/**
* 2. function Str_replace
* Mixed Str_replace (mixed $search, mixed $replace, mixed $subject [, int & $count])
*/
Echo str_replace (' It ', ' Java ', $str);
output:http://flyer0126.javaeye.com/
echo str_replace (Array (' http ', ': ', '//', '/'), ', $str);
Output:flyer0126.iteye.com
echo str_replace (Array (' HTTP ', ' it ', '/'), Array (' HTTPS ', ' Java ', '), $STR);
Output:https:flyer0126.javaeye.com