PHP string substitution function str

Source: Internet
Author: User
Tags functions string

PHP in the string replacement function, there are str_replace (), Str_ireplace (), Substr_replace (), Preg_replace (), STRTR () and so on, the programmer in the process of writing, Often according to their own habits and the actual situation to choose one or more. Although these functions have the function of string substitution, they are different from syntax to function, speed and efficiency, so they are not available on any occasion.

The difference between str_replace () and Preg_replace ()

In the function of string substitution, str_replace () has the highest usage rate, its usage is simpler, and preg_replace () is relatively low in usage, because it requires you to know how to write regular expressions, while regular expressions are sometimes not easy to write.

The use of Str_replace () and Preg_replace () is different, Str_replace is replaced (find) the content is fixed, determined, of course, can use variables, but the variable also represents a fixed, determined content, such as can be done to replace all \ n <br> and other occasions. The content of the Preg_replace (lookup) is described by rules, such as replacing all the content between < and > (HTML code). Of course preg_replace can also be used to replace fixed content.

Str_replace faster than Preg_replace.

Comparing the two, who is better at implementing efficiency? This involves the speed issue. Some tests have found that the str_replace speed is faster than preg_replace, but it seems that some people say 4 times times as much faster. 10w Two word replacement results indicate that str_replacece time consuming 0.38 sec,preg_replace takes 0.97sec.

The difference between str_replace () and Str_ireplace ()

The use of Str_replace () and Str_ireplace () is the same, and the difference is only that str_replace () is sensitive to case, and Str_ireplace () is case-insensitive.

Str_replace () and STRTR () who is faster?

Str_replace () and STRTR () the efficiency of the implementation of the two, many people disagree, some say strtr () than Str_replace () faster, but some have been proven to str_replace () than STRTR () faster.

When a Netizen tests Strtr () in Strtr (String,array) usage, it is slower than str_replace. The two-word replacement of 10w times indicates that str_replacece () takes 0.38 sec, while STRTR () takes 0.52sec.

In the PHP program, there are many functions can be implemented with different functions, in addition to the above several, perhaps we have not noticed, such as a long time ago to share an article:

PHP determines string length isset () faster than strlen ()

So when we choose which function to use, we'd better know how efficient those functions are, so that you can write more performance-better programs.



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.