Regular expression help - limit content within result

Hi, i have some data in the following format:


<div class = "news">
   <div class = "headline">some headline text</div>
   <div class = "body">lots of body text. lots of it. and lots of it.</div>
   <hr />
</div>

I am trying to display a summary of the news on the index page to my site. I would like to keep the above structure but limit the content/body of the news article to the first occurence of a full stop. After the first full stop is found, the rest of the content within that div tag is skipped, and the rest of the structure returned. So with the above example, i would like the following text to be returned:


<div class = "news">
   <div class = "headline">some headline text</div>
   <div class = "body">lots of body text.</div>
   <hr />
</div>

So far i have written a regex that will go to the first full stop in the body, but i do now know how to go any further. Here is what i have:

<div class\\=[\\"]news[\\"]>[\\s]?<div class\\=[\\"]headline[\\"]>(.*?)<\\/div>[\\s]?<div class\\=[\\"]body[\\"]>[\\d\\w\\s]{0,100}.

Any ideas on how to proceed?

So is this coming from your own systems?

Overall, common wisdom seems to point to you using a tool which inspects and traverses the DOM rather than a regex, provided you have the right server setup it seems PHP: DOM - Manual

Sorry, this is not really answering your question directly.

If this occurence only happens a few times a day, and you are caching results then I would have no qualms in doing this in several operations rather than trying to make one regex to do the whole thing.

Thanks for the reply. Yes, there is a news section on the site, where users can write articles, but on the index page i want to display just a summary of the current articles, by displaying the headline and the first sentence or paragraph. As the content is inserted into the database as one big text string, i thought reged would be best to delve into the content to extract what i am after. So it will be happening every time the index page is loaded, analysing 5 articles. That may be too much overhead to use the DOM extension, although i hadn’t thought about caching it.

OK, so the layout of the target data stream is entirely predictable.

(I suppose the big question is really why don’t you treat these element separately and store them in their own fields from the get-go, but I will presume that you have your reasons)

for a starter I would chop the first 300 characters (say) in your sql query and simply split at ‘<div class=“body”>’.

Now you have the start of the string, a finite amount of characters remain, say between 1 and 2 hundred, and then split again at full stops.

If the first part of that split is worryingly short, then include the second sentence too.

This might be a starter, at least it isolates the first part of your string, and leaves you with another explode to do on the full stop on $parts[1]


$tgt = '<div class = "news">
   <div class = "headline">some headline text</div>
   <div class = "body">lots of body text. lots of it. and lots of it.</div>
   <hr />
</div>';

$parts = explode('<div class = "body">', $tgt );

var_dump( $parts );

/* gives
array
  0 => string '<div class = "news">

   <div class = "headline">some headline text</div>

   ' (length=78)
  1 => string 'lots of body text. lots of it. and lots of it.</div>

   <hr />

</div>' (length=71)
*/

Is this going in the right direction?