0

There is probably a very simple answer to this, but I want to be as detailed as possible so that you do not need me to clarify.

I am trying to collect the contents of every

<content><div>CONTENT</div></content>

The content needs to be returned as a backreference ($1). Both the content and the div have differing parameters (such as style="color: white;"). These parameters are unimportant, but exist nonetheless.

The complication is that the div may contain child div's. These are not important, but conflict with my current regex - stopping the match early.

Here is a sample of the code, imagine this copy/pasted several times and formatted differently.

<entry> 
<title>A general title of a post</title> 
<content type="xhtml"> 
    <div xmlns="http://www.w3.org/1999/xhtml"> 
    This is a description of the title. It may <b>contain bold text</b> or <div>even divs</div>, and everything else. It is not quite important to save these tags, but they exist nonetheless.
    </div> 
</content> 
</entry>

Currently, I am using two regex codes. One for the declaration, and one for the closing tags. This works, but now I need to execute code on the contents. So, I will use preg_replace_callback(), but I can't figure out how to connect the two so that the middle is a callback.

Declaration:

<content \w+\s*=\s*\".*?\">[\r\n\s]{0,}<div \w+\s*=\s*\".*?\">

Closing:

</div>[\r\n\s]{0,}</content>

I need these combined, with the contents returned as a callback. I have tried something like ([\w\W]{0,}), which returns absolutely everything, but this match doesn't stop at the closing div.

So I found out about the \bFULLWORD\b command, and threw \bdiv\b on that... But I have had no success getting that to work. Perhaps it is not supported by PHP? Or I am stupid.

I do not know.

Please help!

3
  • 8
    Do not use regex to manipulate HTML : use DOM -- see the DOMDocument class : fr2.php.net/domdocument Commented Mar 18, 2011 at 12:32
  • 2
    Please see link Commented Mar 18, 2011 at 12:44
  • I must agree with the ^^ "He said Jehova!" this time. Matching nested tags is possible only with heaps of effort and recursive regexps. Use the simpler approach and for example phpQuery or QueryPath to extract contents. qp($html)->find("content div")->text(); Commented Mar 18, 2011 at 12:48

2 Answers 2

2

It's been said before and it's being said now, and unfortunately it's going to be said again. Regex is a wonderful tool. It's great for manipulating strings and pattern matching of regular expressions.

HTML is not a string. HTML is a markup language, not a regular language. It's not truthfully a string, but can be interpreted as one (and thus, why we can technically use regex to manipulate HTML). HTML is it's own language based on element nodes, you need to manipulate those elements if you're going to change something.

As pointed out in the comments, you can easily use the DOM class in PHP.

You want to do this for quite a few reasons:

  • It's easier, you don't need to make some crazy pattern that looks like a cat walked across your keyboard
  • It's easier (again), you can navigate to the specific node, not work with the whole document.
  • It's safer, you don't accidentaly change something you didn't want to
  • It's safer (again), the source data can change, and you can detect it and account for it.
  • It's safer (again again), you can fail gracefully.

How?

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think it's appropriate to "answer" a question without actually answering it. This may be your idea of best practice, but it does not contribute the the OP's working knowledge of regular expressions (and it comes off a tad elitist).
-2

Use a DOM parser. Here's an example: http://htmlparsing.com/php.html

1 Comment

This is a "moral" answer rather than an answer to the actual question. It contributes nothing to a working knowledge of regular expressions.

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.