Php string replacement function str_replace is faster than preg_replace
Source: Internet
Author: User
String replacement functions in php include str_replace (), str_ireplace (), substr_replace (), preg_replace (), and strtr (). when programmers write programs, you can select one or more of them based on your habits and actual conditions. Although these functions all have the character string replacement function, they include str_replace (), str_ireplace (), and str_ireplace () functions in php, no matter from syntax to function, or speed and efficiency (), substr_replace (), preg_replace (), strtr (), etc. when writing a program, programmers often choose one or more of them based on their own habits and actual conditions. Although these functions have the character string replacement function, they are different in terms of syntax, speed, and efficiency. Therefore, they are not used in any scenario.
Difference between str_replace () and preg_replace ()
In the string replacement function, the usage of str_replace () is the highest, and its usage is also relatively simple, while the usage of preg_replace () is relatively low, because it requires you to know how to write regular expressions, and regular expressions are sometimes not easy to write.
The usage of str_replace () and preg_replace () is different. the content of str_replace replaced (search) is fixed and definite. of course, variables can be used, however, variables also indicate fixed and definite content. for example, you can replace all \ n
. The content of preg_replace replaced (search) is described by rules. for example, all <和> (HTML code. Of course, preg_replace can also be used to replace fixed content.
Str_replace is faster than preg_replace
In comparison, who is better at execution efficiency? This involves speed issues. Some tests have found that str_replace is faster than preg_replace, but it does not seem as many as four times faster. The results of 0.38 replace of two words show that str_replacece takes seconds, and preg_replace takes seconds.
Difference between str_replace () and str_ireplace ()
The usage of str_replace () and str_ireplace () is the same. The difference is that str_replace () is case sensitive, while str_ireplace () is case insensitive.
Who is faster than str_replace () and strtr?
The execution efficiency of str_replace () and strtr () is different from that of many people. some people say that strtr () is faster than str_replace (), but some instances prove str_replace () faster than strtr.
When a user tests strtr () usage in strtr (string, array), it is slower than str_replace. The result of 0.38 replications of two words indicates that str_replacece () takes sec, while strtr () takes sec.
In the php program, many functions can be implemented using different functions. apart from the above, you may not have noticed them. for example, an article I shared a long time ago:
Php judges string length isset () faster than strlen ()
So when we choose what function to use, we 'd better know the execution efficiency of those functions so that we can write programs with better performance.
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