Skip to content
Advertisement

Insert spaces into a string at each change of character

I’m trying to create a string with a blank space between all “different characters, for example:

JavaScript

I tried the following recursive function, not knowing if this is the best way because I couldn’t find any build-in function in PHP for this.

JavaScript

For some reason, this function seems to iterate infinitely, and I can’t figure out why. Where do I go wrong?

Is there a better way to do this? I want to use explode to out the answering string in an array, can this be done directly?

Advertisement

Answer

RegEx approach,
In RegEx 1 it’s a backreference for what has been captured by d

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