replacer Introduction to the use of strtr function in PHP str_replace

WBOY
Release: 2016-07-29 08:46:59
Original
1216 people have browsed it

strtr has two forms:
string strtr ( string $str , string $from , string $to )
string strtr ( string $str , array $replace_pairs )
When using the first type, the parameters $from, $to The length of the strings must be the same, otherwise the extra characters (whether there are more $from or more $to) characters will be ignored.
For example, $str = 'a-=b' ;
When $from='-=' ,$to= 'CD', output 'aCDb', because '-=' and 'CD' have the same length, there is no problem.
When $from='-=', $to='CDE', output 'aCDb', in $to 'E' is ignored.
When $from='-=', $to='C', 'aC=b' is output, and the '=' in $from is ignored.
When using the second form, then Without this problem, extra entries will not be ignored.
So, if you deliberately use the strtr function instead of str_replace, and use the first form, you must pay attention to this feature, which may be a trap.

The above introduces the use of strtr function in replacer and str_replace in PHP, including the content of replacer. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template