Skip to content
Advertisement

php str_replace replacing itself

I need to replace every occurrence of one of the letters a,o,i,e,u with [aoieu]?
I tried to do the following:

JavaScript

But when giving it input of black instead of giving me the expected bl[aoieu]?ck it gave me

JavaScript

How can I get it to not replace things it already replaced?

Advertisement

Answer

You can consider using a regular expression for this, or you can make your own function which steps through the string one letter at a time. Here’s a regex solution:

JavaScript

Or your own function (note that $search only can be a single char or an array of chars, not strings – you can use strpos or similar to build one which handles longer strings as well):

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement