Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP regex - find and replace

Tags:

regex

php

I am trying to do this regex match and replace but not able to do it.

Example

<SPAN class="one">first content here</SPAN>
<SPAN class="two">second content here </SPAN>
<SPAN class="three">one; two; three; and more.</span>
<SPAN class="four">more content here.</span>

I want to find each set of the span tags and replace with something like this

Find

<SPAN class="one">first content here</SPAN>

Change to

<one>first content here</one>

same way the the rest of the span tags.

class="one", class="two" and so on are the only key identifier which I use in the regex match expression. So if I find a span tag with these class then I want to do the replace. My main issue is that I am not able to find the occurrence of first closing tag so what it does is it finds from the start to end which is of no use. So far I have been trying to do this using notepad++ but just found that it has its limitations so any php help would be appreciated.

regards

like image 733
geej Avatar asked Dec 18 '22 01:12

geej


1 Answers

preg_replace('/<span class="(.*?)">(.*?)<\/span>/i', '<$1>$2</$1>', $input);
like image 96
Amy B Avatar answered Dec 28 '22 23:12

Amy B