Getting <div> content from HTML
Posted: Sat Jul 04, 2009 2:07 pm
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:
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
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));
?>
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