0

There is an html document with the following format:

<div....>
  <map name="blah"
           .
           .
   />
  <map name="blah2"
          .
          .
   />
</div>

I want always to retrieve the second map. However, I want to make completely dynamic.

$url = $_GET['url'];
$html_content = getHTML($url);
$html = str_get_html($html_content);

$map = $html->find('map[name=blah2]');

The aforementioned lines are working perfectly fine. But as I mentioned before I don't want to give manually the name. I just want always to take the second map. And moreover I want to retrieve also the name of the map.

Any ideas?

p.s. The code bellow doesn't work. I've tried this before. And doesn't display the content under the map. However, correctly returns the name of the map

   $map = $html->find('map',1);
Nick Robertson
  • 1,047
  • 4
  • 18
  • 41

3 Answers3

1

What about:

$map = $html->find('map', 1);
echo $map->name;
sroes
  • 14,663
  • 1
  • 53
  • 72
1

It is very easy:

$map = $html->find('map',1);
if($map != null)
    $name = $map->name;

You just had to look.

Prasanth
  • 5,230
  • 2
  • 29
  • 61
1

You could use a port of jQuery such as PHPQuery http://code.google.com/p/phpquery/ which would give you the eq() selector and will enable fairly rich manipulation of XML in general

Sqoo
  • 5,197
  • 2
  • 17
  • 18
  • +1 thanks! I didn't know of this. It looks like a very good project [~500 stars]. – Prasanth Nov 21 '12 at 15:55
  • the only problem is it appears to have died as a project :/ however I have used in on a very complex CMS system that manipulated and built HTML in some mind-bending ways, and never ran into any major problems. – Sqoo Nov 21 '12 at 16:53