Skip to content
Advertisement

PHP: Replace the content between two characters, in multiple occurrences

I’m trying to replace the contents between some “special” characters, in each of their occurrences. For example, let’s say i have that string:

JavaScript

I would like to replace each “special content”, between square brackets, with something that is represented by this “identifier”(let’s say, some “widget”).

I’ve tried this code from Stackoverflow:

JavaScript

This works, but only for the first occurrence. I need this operation to be repeated across the entire string.

I’ve also tried this one:

JavaScript

It gives me a “Warning: preg_replace(): Unknown modifier ‘d’ in Standard input code on line 8” error.

Any ideas?

Advertisement

Answer

Your code from stackoverflow needs minor changes:

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