0

My PHP script can fetch content from a div id, but what is the way to filter this fetch data and exclude some of its content which has this div id <div id="navbar" class="n"> I have tried with this code but its not working

$regex = '#\<div id="navbar"\>(.+?)\<\/div\>#s';
preg_match($regex, $displaybody, $matches);
$match = $matches[0];
echo "$match";`

To fetch content i am using HTML DOM Parser.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
user1642787
  • 75
  • 1
  • 2
  • 7
  • How do are you doing the parsing? You might be able to write a rule there instead having to use regex. – Stegrex Sep 04 '12 at 04:39

1 Answers1

1

Using regexpes to parse html is usually a bad idea. You can select nodes with the DOM just fine:

$input = '<html> <body> some content <span class="a">b</span> <div id="navbar" class="n">find me <span class="a">b</span></div> </html>';

$doc = new DOMDocument;
$doc->loadHTML($input);
$navbar = $doc->getElementById('navbar');

$innerhtml = '';
foreach ($navbar->childNodes as $cn) {
    $innerhtml .= $doc->saveHTML($cn);
}
print $innerhtml;
Community
  • 1
  • 1
complex857
  • 20,425
  • 6
  • 51
  • 54
  • i dont know why its not working here i have include this http://sourceforge.net/projects/simplehtmldom/files/ what to do now? – user1642787 Sep 04 '12 at 13:47
  • simpledom is not the same as the DOM extension, you should be able to use the same input for `DOMDocument::loadHTML` as you used for the `new siple_html_dom()`'s fist parameter. – complex857 Sep 04 '12 at 14:20
  • i dont know where goes wrong, it shows nothing but a blank page. Can u pls edit your code. Thanks – user1642787 Sep 04 '12 at 14:57
  • I'm not sure what goes wrong, take a look at this [demo](http://codepad.viper-7.com/jdvtth). Could it be that you php installation lacks the DOM extension? (check phpinfo) – complex857 Sep 04 '12 at 15:26