PHP strtr Function and str_replace performance comparison _php tutorial

Source: Internet
Author: User
PHP's STRTR function, the performance is higher than the Str_replace function, can replace the str_replace to use. There are two forms of STRTR:


String Strtr (String $str, String $from, String $to)
String Strtr (string $str, array $replace _pairs)
When the first type is used, the arguments $from, $to string lengths must be the same, otherwise the extra (either $from or $to) characters are ignored.


For example $str = ' a-=b ';


When $from= '-= ', $to = ' CD ', Output ' aCDb ' because '-= ' is the same length as ' CD ', no problem.


When $from= '-= ', $to = ' CDE ', Output ' aCDb ', $to ' E ' is ignored.

When $from= '-= ', $to = ' C ', Output ' ac=b ', $from ' = ' is ignored.

Instead of using the second form, there is no problem, and the extra note is not ignored.


Therefore, if you deliberately replace str_replace with the STRTR function and use the first form, you must pay attention to this feature, which may be a trap.

http://www.bkjia.com/PHPjc/632263.html www.bkjia.com true http://www.bkjia.com/PHPjc/632263.html techarticle PHP's STRTR function, performance is higher than the Str_replace function, can be used instead of str_replace. STRTR in two forms: String Strtr (String $str, string $from, str ing $to) string St ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.