Getting <div> content from HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Squibbles1077
Forum Newbie
Posts: 8
Joined: Sat Jul 04, 2009 2:00 pm
Location: US

Getting <div> content from HTML

Post by Squibbles1077 »

What I'm trying to do is make an RSS feed that automatically updates when my site updates. I have the titles of each post in <div class="main"> tags but I have been unable to figure out how to get that content from a php script.

Here's what I have so far:

Code: Select all

<?php
$data = file_get_contents('http://www.mywebsite.com/');
preg_match_all ("/<div class=\"main\">([^`]*?)<\/div>/", $data, $matches);
//testing the array $matches
echo sizeof($matches);
echo sprintf('<pre>%s</pre>', print_r($matches, true)); 
?>
 
which outputs this:
2

Array
(
[0] => Array
(
)

[1] => Array
(
)

)


It's creating a 2d array in $matches, which is excellent, but $matches[0] and [1] contain no data. I'm assuming that something is wrong with the regex in the preg_match_all but I'm not extremely familiar with regular expressions so that might not be it. I know it's getting the HTML code just fine, because if I echo $data it shows the contents of mywebsite.

Thanks
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Getting <div> content from HTML

Post by prometheuzz »

Post Reply