What is the preg_replace regex to replace this HTML tag?

preg_replace('/<span class="it">(.*?)<\/span>/', '{it}$1{/it}', $text)

This is not the most versatile solution, but this works for your code. There is the possibility to have the content of the class attribute as a variable as well, but that won't be too hard to figure out now.


The following should get you started:

preg_replace('#<span\s*class="(\w*)">(\w*)</span>#i', '{$1}$2{/$1}', $str);

Output:

{it}CONTENT{/it}

Regex101 Demo


Try this

 preg_replace('/<span.+class="(.+)">(.+)<\/span>/', '{$1}$2{/$1}', $text);