0

We have html page, it has many code inside.

Sometimes it has block #container, sometimes not.

All code of this page is inside $page variable.

How do I:

  1. check, is there block with id="container"?
  2. if html page has #container inside, then get it contents of this block and write to a variable $container.

Task should be done by php.

Hubert Kario
  • 21,314
  • 3
  • 24
  • 44
James
  • 42,081
  • 53
  • 136
  • 161
  • 4
    There are plenty of [HTML parsers](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php) available for PHP. – NullUserException Mar 03 '11 at 19:32
  • @NullUserException and they are slow as hell. Given that an id can only be used once, use a regex to check for `id="container"` then only take the performance hit of firing up the parser if necessary. – Endophage Mar 03 '11 at 19:40

2 Answers2

5

One of the possible ways to solve your problem is to use third party library. Let's say http://simplehtmldom.sourceforge.net/:

$html->load($page);
if ($html->find('#container')) $container = $html->find('#container');
akrisanov
  • 3,212
  • 6
  • 33
  • 56
-2

Regexp is your friend here.

Good luck for your html parsing trip

Shahor
  • 365
  • 1
  • 2
  • 10