Comparison between PHP strtr and Str_replace

Source: Internet
Author: User

Comparison between PHP strtr and Str_replace

Functions have the ability to replace characters. But the STRTR is 4 times times more than the str_replace performance. Specific circumstances, please

Look at the following decomposition:

The first is the STRTR function:

Example 1: When

The following are the referenced contents:

<?php

This time the output is Baicai rather than Bai123cai, because str ("Pao") <strlen ("bai123")

Echo strtr ("paocai!", "Pao", "bai123");

?>


Instance 2: When the replaced value is less than the replacement target

The following are the referenced contents:

<?php

This time the output is Laocai rather than Lacai, because str ("Pao") >strlen ("La")

Echo strtr ("paocai!", "Pao", "la");

?>


Example 3: Array substitution support

The following are the referenced contents:

<?php

$Arr =array (' ao ' => ' oa ', ' ai ' => ' ia ');

Echo strtr ("paocai!", $ARR); This time the output is Poacia

?>


The second is str_replace:

The following are the referenced contents:

<?php

Echo str_replace ("You", "Paocai", "I Love you!"); Will output I love paocai!

?>


Summary: STRTR He is related to the length of the character, but Str_replace is not related, estimated in the running step

Will read the string length so it is much slower than STRTR.

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.