0

Let's say I have the following code:

<p class="some_class">text</p>

I need to replace it with something like this:

<div class="other_class">
    <img src="line.png" class="line">
    <span class="comm">text</span>
</div>

I guess that the best way to do this is with regular expressions, but I have no idea how to use them here. I'm using PHP. Basically, I need to match the text wrapped with p tag of specific class, and insert it into the other tags.

Thanks in advance.

  • 1
    The best way to do this with `DOMDocument`. – Barmar Dec 27 '14 at 17:35
  • 1
    Welcome to Stack Overflow. No, Regex is likely not the best way to parse HTML (see http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags for our canonical example). @Barmar likely has it right. – Andrew Dec 27 '14 at 17:49
  • "I guess that the best way to do this is with regular expressions" - Wrong guess, this does not warrant asking a `please send the regex TIA` question. Use Javascript. – Unihedron Dec 27 '14 at 17:55
  • Why not go into the src file, and change it manually there? – a coder Dec 27 '14 at 18:20

1 Answers1

1

Your question is not so clear, but if I understand well you want to change some elements in HTML as replace, remove, add etc. If this is your problem I most to say @andrew was right. Additionally I can suggest you using some sort of HTML Parser for example : http://htmlparser.sourceforge.net/ or HTML Sanitizer extensions like one deployed by Google you can find it at: https://code.google.com/p/google-caja/wiki/JsHtmlSanitizer or you can search for other sources.

QMaster
  • 3,743
  • 3
  • 43
  • 56