What is the fastest string replacement function in PHP? sprintf() is probably the fastest, but it’s only useful in limited situations where the input is prepared for sprintf formatting. preg_replace() is obviously the slowest. The big debate is whether str_replace() or strtr() is faster.
The lixlpixel PHP benchmarks show that strtr is the faster of the two. I then came across Simple Machine’s benchmarks which show that str_replace() is faster. Hmm what’s going on?
Syridium DV’s PHP benchmarks performs more varied tests with these two functions. The relative speed of these two functions might vary depending on the type of string replacement you are performing. However, it seems that str_replace() is usually a safe bet.
If you liked this post, please subscribe to my feed. Thanks for visiting!

