Here given a string and the task is to insert a character after every n characters in that string. Here are 2 examples discussed below.
Approach 1: In this approach, the string is broken into chunks by using substr() method and pushed to an array by push() method. The array of chunks is returned which then joined using join() method on any character.
Approach 2: In this approach, a RegExp is used which selects the parts of string and then joined on any character using join() method.
- PHP | Ds\Sequence insert() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.