-2

I want to extract data in between <div id="navigation"></div> tags using regular expression from html content(string). Any help is appreciated.

`$html = load($url);

preg_match_all ("/<div>(.+?)<\/div>/is",
            $html, &$matches);



$matches = $matches[1];
$list = array();

foreach($matches as $var)
{  
    print("<br>");  
    print($var."<br>");
}`

Here I want to retrieve data from a specific div tag.

hakre
  • 193,403
  • 52
  • 435
  • 836
Kishor Kumar
  • 543
  • 1
  • 12
  • 33
  • 2
    Do NOT use regexes on HTML: http://stackoverflow.com/a/1732454/118068. And... what have you tried so far? We're not going to write something from scratch for you, you have to prove you've at least TRIED to do something first. – Marc B Apr 18 '12 at 05:07

3 Answers3

1

I think the easiest way to to it is by using jquery or you can try this html php phraser

http://simplehtmldom.sourceforge.net/

COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
1

Using Jquery

Try this code

<div id='navigation'>
   <p >
       Navigation div contents
  </p>
</div>

// Javascript

var test= $('#navigation').clone();
alert(test.text());

Naveen Kumar
  • 4,543
  • 1
  • 18
  • 36
0

As stated: You can't parse [X]HTML with regex.

$url = "http://yo.ur/url/goes/here";

$dom = new DOMDocument();
$dom->loadHTML(file_get_contents($url));
$dom->preserveWhiteSpace = false;
$element = $dom->getElementById('navigation');
echo $element->nodeValue;

Also, PHP load()? Are you kidding me?

Community
  • 1
  • 1
Marco Gamba
  • 322
  • 2
  • 5
  • load() is a custom class function which gives me content of webpage. – Kishor Kumar Apr 19 '12 at 06:52
  • Then it should be kind of `$instance->load()`, shouldn't it? And how about `file_get_contents()`? By the way, is there anything wrong with my code? – Marco Gamba Apr 19 '12 at 12:38
  • load() is giving appropriate result i.e html in the form of string. Thats not a problem. I'm sending first form result to a php file where `file_get_contents()` is working fine but when I'm sending second form data to another php file, `file_get_contents()` is not working. – Kishor Kumar Apr 19 '12 at 14:01