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.

2
  • 4
    There are plenty of HTML parsers available for PHP. Commented Mar 3, 2011 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. Commented Mar 3, 2011 at 19:40

2 Answers 2

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');
Sign up to request clarification or add additional context in comments.

Comments

-2

Regexp is your friend here.

Good luck for your html parsing trip

1 Comment

Regex is a terrible solution for parsing HTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.