4

This is mycode

<?php

/**
 * @author Joomlacoders
 * @copyright 2010
 */
    $url="http://urlchecker.net/html/demo.html";

    $innerHtml=file_get_contents($url);

    //echo $innerHtml;
    preg_match_all("{\<div id='news-id-.*d'\>(.*)\</div\>}",$innerHtml,$matches);

          //<div id='news-id-160346'>            

    var_dump($matches);

?>

I want find all content in div id='news-id-160346'. Please help me

skaffman
  • 398,947
  • 96
  • 818
  • 769
Thoman
  • 742
  • 2
  • 9
  • 20

2 Answers2

6

Use an HTML parser. NOT regular expressions.

The problem with regular expressions is that they cannot match nested structures. Assuming your regex must match a single <div> and its closing tag, there is no way to correctly match this input:

<div id="a">
    <div id="b">
        Foo
    </div>
</div>
<div id="c">
    Bar
</div>

Because if your regular expression is greedy, it will match the two uppermost divs, and if it's ungreedy, it will not match the correct end tag.

Therefore, you should use an HTML parser. With PHP, DOMDocument::loadHTML or DOMDocument::loadHTMLFile each do a fairly good job. (You may "safely" ignore the warnings it generates: they're only markup errors, and the generated DOMDocument object should be pretty much okay.)

Since the PHP getElementById is a pain to get to work, you can use DOMXpath for the same purpose:

<?php

$url = "http://urlchecker.net/html/demo.html";

$d = new DOMDocument();
$d->loadHTMLFile($url);

$xpath = new DOMXPath($d);
$myNews = $xpath->query('//@id="news-id-160346"')->item(0);

?>
Community
  • 1
  • 1
zneak
  • 134,922
  • 42
  • 253
  • 328
  • Hello I had try all answer but not successful Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: Unexpected end tag : a in http://urlchecker.net/html/demo.html, line: 26 in /home/urlcheck/public_html/html/test.php on line 10 – Thoman Jun 01 '10 at 05:59
  • 1
    @Thoman: it's actually been successful. loadHTMLFile simply tells you the problems it encountered while parsing. You can shut it up with the `@` operator: `@$d->loadHTMLFile($url);` – zneak Jun 01 '10 at 06:26
  • I try it but this code don't matches all content in id='news-id-160346' – Thoman Jun 01 '10 at 07:13
0

Use a parser as others suggested.

Or try this regex:

preg_match_all("#<div [^>]*id=['\"]news-id-\\d+['\"](.*?)</div>#", $innerHtml, $matches);
print_r($matches);

Check the output of the print_r statement to understand why regex is not considered as the right tool for parsing html.

Amarghosh
  • 58,710
  • 11
  • 92
  • 121