Fastest way to replace string in PHP

I need to replace some string in a PHP code to make it look better. What would be the best way to go about that?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

The usual suspects would be str_replace and preg_replace.

Solution 2

preg_replace

Is a good way to go and uses regular expressions so you have flexibility as to how to do it.

Solution 3

If you want to replace a string in a lot of files once (e.g., you have something in your source code which you want to replace), sed might be the right tool for the job.

man sed

Solution 4

It depends. If str_replace works for your case, I think is faster and less memory consumer than preg_replace

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply