mb_str_replace()... is slow. any alternatives?

onassar picture onassar · Aug 16, 2010 · Viewed 30.5k times · Source

I want to make sure some string replacement's I'm running are multi byte safe. I've found a few mb_str_replace functions around the net but they're slow. I'm talking 20% increase after passing maybe 500-900 bytes through it.

Any recommendations? I'm thinking about using preg_replace as it's native and compiled in so it might be faster. Any thoughts would be appreciated.

Answer

Áxel Costas Pena picture Áxel Costas Pena · Jan 14, 2013

As said there, str_replace is safe to use in utf-8 contexts, as long as all parameters are utf-8 valid, because it won't be any ambiguous match between both multibyte encoded strings. If you check the validity of your input, then you have no need to look for a different function.