PHP string substitution function str_replace faster than preg_replace

Source: Internet
Author: User

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

The difference between str_replace () and Preg_replace ()

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

Str_replace () and preg_replace () are different, Str_replace is replaced (find) the content is fixed, OK, of course, can use variables, but the variable also represents a fixed, determined content, such as can be done to replace all \ n

and other occasions. The content of Preg_replace is replaced by rules, such as the ability to replace all the contents (HTML code) between < and >. Of course preg_replace can also be used to replace fixed content.

Str_replace faster than Preg_replace.

Who is better at implementing efficiency? This involves the speed issue. Some tests have found that str_replace speed is faster than preg_replace, but it seems that no one has said it as much as 4 times times faster. 10w times the two word substitution results show that: str_replacece time consuming 0.38 sec,preg_replace time consuming 0.97sec.

The difference between str_replace () and Str_ireplace ()

The use of Str_replace () is the same as Str_ireplace (), where the difference is only str_replace () is case sensitive, and str_ireplace () is not case sensitive.

Str_replace () and STRTR () who's faster?

Str_replace () and STRTR () the efficiency of the implementation of the two, many people say that strtr () than Str_replace () faster, but some people have proved by example Str_replace () faster than STRTR ().

When a Netizen tests Strtr () in Strtr (String,array) usage, it is slower than str_replace. 10w times the two word substitution results show that str_replacece () takes 0.38 sec, while STRTR () takes 0.52sec.

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

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

So when we choose what function to use, we'd better know how efficient those functions are, in order to write a better-performing program.

May 3 Oil price Latest trend Chart "International crude oil price": http://licai.daiyuline.com/yyjg/331.html

May 3 Stock Market Analysis and forecast "newcomer knowledge of Stocks": http://licai.daiyuline.com/gupiao/330.html

Big rebound will not be staged today "Octopus investment Banking": http://licai.daiyuline.com/gupiao/329.html

PHP string substitution function str_replace faster than preg_replace

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.