5

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

3
  • Hi, I forgot to put code tag so the q is messed up. please read it below in the reply post. Commented Apr 7, 2010 at 16:18
  • You know you can edit your post if you see a mistake, you don't need to post the fix as an answer. Commented Apr 7, 2010 at 16:23
  • I am sorry, I tried to look for that option but couldnt find it. Please let me know and I can delete the answer post and correct the question. Commented Apr 7, 2010 at 16:28

1 Answer 1

13
preg_replace('/<span class="(.*?)">(.*?)<\/span>/i', '<$1>$2</$1>', $input);
Sign up to request clarification or add additional context in comments.

1 Comment

but there is a typo. should add a ) before the double quotes ends.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.