Chirag
Chirag

Reputation: 1929

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?

Upvotes: 6

Views: 8120

Answers (4)

dotoree
dotoree

Reputation: 2993

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

Upvotes: 0

Emil Vikström
Emil Vikström

Reputation: 91942

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

Upvotes: 0

spinon
spinon

Reputation: 10847

preg_replace

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

Upvotes: 0

Joey
Joey

Reputation: 354516

The usual suspects would be str_replace and preg_replace.

Upvotes: 8

Related Questions