I’m trying to retrieve specific tags with their content out of an xhtml document, but it’s matching the wrong ending tags.
In the following content:
<cache_namespace name="content">
<content_block id="15">
some content here
<cache_namespace name="user">
<content_block id="welcome">
Welcome Apikot!
</content_block>
</cache_namespace>
</content_block>
</cache_namespace>
The content_block ending tag for id=”welcome” actually get’s matched as the ending tag of the first opening content_block tag.
The regex I’m using is:
/<content_block id="(.*)">([\w\W]*?)<\/content_block>/i
Any pointers as to where I’m failing?
… and the answer is always the same: HTML + regex cannot be done. Sorry. Use an HTML parsing library for your particular framework. Or, if your document is guaranteed to only contain valid XHTML, take the XPath approach as proposed by jitter in a comment.