使用 PHP 获取 XHTML 地图区域坐标

发布于 2024-12-01 10:27:33 字数 408 浏览 0 评论 0原文

我在从下面的区域地图示例中获取属性时遇到问题,

  <area shape="poly" class="areaSelect" coords="475,241,495,220,515,215,531,226,534,262,530,290,493,307,472,287" href="http://www.someURL" title="Area Title" alt="Link Title"/>

我一直在使用 simple_html_dom.php 从页面中检索其他元素。任何帮助将不胜感激。因为我一直在这方面反复思考,所以我确实尝试使用 Xpath 并加载 XHTML 文件,但是当我尝试获取坐标时,我只得到了 42 个“DOMAttr Object ()”案例,地图上有 42 个区域,但是.. .... 帮助!

I am having trouble getting the attributes from within an Area Map example below

  <area shape="poly" class="areaSelect" coords="475,241,495,220,515,215,531,226,534,262,530,290,493,307,472,287" href="http://www.someURL" title="Area Title" alt="Link Title"/>

I have been using simple_html_dom.php to retrieve other elements from the page. Any help would be greatly appreciated. As I have been going around and around on this I did try using Xpath and loading the XHTML file but when I tried to get the coords I only got 42 cases of "DOMAttr Object ()" there is 42 areas on the map but...... help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻许诺言 2024-12-08 10:27:33

好吧,我会尽力为其他想做类似事情的人提供尽可能多的细节。

这是一个快速示例文件。

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Untitled Document</title>
  <style>
   body{font:Arial, Helvetica, sans-serif;}
  </style>
 </head>
 <body>
 <div class="title" style="font-size: 14px; color: #009"><h2>Get any information</h2>     </div>
 <div id="image-holder">
<img src="http://mrg.bz/sb3gG9" alt="microchips" width="496px" height="343px" border="0" usemap="#MicroChips"/>
<map name="MicroChips" id="MicroChips2">
    <area class="areaSelect" shape="poly" coords="46,91,157,43,217,106,101,159" href="http://www.someplace.com" title="Main Chip" alt="Main Chip" />
</map>
<div id="body" style="width:496px">
    <p>This is to demonstrate how to get as much out of a file as possible. Well a least some of it anyway.</p>
    <p>Here is another paragraph to show how to get multiple items</p>
</div>
 </div>
 </body>
 </html>

好的,现在这是 php.ini。您需要下载“PHP Simple HTML DOM Parser”并将其加载到您的测试服务器上。以便您可以访问附加功能。

 <?php
    include('simple_html_dom.php'); //Load/include the PHP Simple HTML DOM Parser

    $html = file_get_html('http://www.YourServer.com/test.html'); // Load your html file

    // create a series of variables to call on later

    $imgtitle = $html->find('div.title', 0)->plaintext; //here we reference the HTML file, and find the div with the class "title" and display it as plaintext

   // ok for some reason and hopefully someone out there can add why
   // I had to reference the fist instance of each element here 
   // referenced as the ',0' in each case. 

   // here you can see we search for the div 'image-holder'
   // and then search in that div for the first image and save it to a variable

    $imageurl =  $html->find('#image-holder',0)->find('img',0); 

   // here we target the image scr, width, height, style, alt, border and usemap and assign them to variables

    $imagesrc = $imageurl->src;
    $imageWidth = $imageurl->width;
    $imageHeight = $imageurl->height;
    $imageStyle = $imageurl->style;
    $imageAlt = $imageurl->alt;
    $imageBrd = $imageurl->border;
    $imageUsemap = $imageurl->usemap;

    $map = $html->find('map',0); // here we look for the map and assign it to a variable

    // here we target the map ID and Name and usemap and assign them to variables

$mapID = $map->id;
$mapName = $map->name;

    // we assigned a class to the map areas to find them easier
    $imgmaparea = $html->find('.areaSelect');

    // ok although the example only has one map area this will find multiple
    // areas and print them on the screen using the 'foreach' function 
    foreach ($imgmaparea as $value){
     $areaShape = $value->shape;
     $areaCoords = $value->coords;
     $areaHref = $value->href;
     $areaTitle = $value->title;
     $areaAlt = $value->alt;
      // print them on the screen
         echo 'Shape: '.$areaShape. '<br>';
     echo 'Coords: '.$areaCoords. '<br>';
         echo 'Href: '.$areaHref. '<br>';
         echo 'Title: '.$areaTitle. '<br>';
         echo 'Alt: '.$areaAlt. '<br>';

   }
         // print them on the screen
         echo 'name: '.$mapName. '<br>';
         echo 'Id: '.$mapID. '<br>';
         echo 'filename: '.$filename. '<br>';
         echo 'Src: '.$imagesrc. '<br>';
         echo 'Width: '.$imageWidth. '<br>';
         echo 'Height: '.$imageHeight. '<br>';
         echo 'Style: '.$imageStyle. '<br>';
         echo 'Alt: '.$imageAlt. '<br>';
         echo 'Usemap: '.$imageUsemap. '<br>';

   ?>

我已尝试彻底解释,但如果有人有任何进一步的问题或建议,我很乐意在这里提出

Ok I'll try and give as much details as I can for anyone else who wants to do something similar.

Here is a quick example file.

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Untitled Document</title>
  <style>
   body{font:Arial, Helvetica, sans-serif;}
  </style>
 </head>
 <body>
 <div class="title" style="font-size: 14px; color: #009"><h2>Get any information</h2>     </div>
 <div id="image-holder">
<img src="http://mrg.bz/sb3gG9" alt="microchips" width="496px" height="343px" border="0" usemap="#MicroChips"/>
<map name="MicroChips" id="MicroChips2">
    <area class="areaSelect" shape="poly" coords="46,91,157,43,217,106,101,159" href="http://www.someplace.com" title="Main Chip" alt="Main Chip" />
</map>
<div id="body" style="width:496px">
    <p>This is to demonstrate how to get as much out of a file as possible. Well a least some of it anyway.</p>
    <p>Here is another paragraph to show how to get multiple items</p>
</div>
 </div>
 </body>
 </html>

Ok now here is the php. You will need to download 'PHP Simple HTML DOM Parser' and load it onto your test server. so that you can access the additional functions.

 <?php
    include('simple_html_dom.php'); //Load/include the PHP Simple HTML DOM Parser

    $html = file_get_html('http://www.YourServer.com/test.html'); // Load your html file

    // create a series of variables to call on later

    $imgtitle = $html->find('div.title', 0)->plaintext; //here we reference the HTML file, and find the div with the class "title" and display it as plaintext

   // ok for some reason and hopefully someone out there can add why
   // I had to reference the fist instance of each element here 
   // referenced as the ',0' in each case. 

   // here you can see we search for the div 'image-holder'
   // and then search in that div for the first image and save it to a variable

    $imageurl =  $html->find('#image-holder',0)->find('img',0); 

   // here we target the image scr, width, height, style, alt, border and usemap and assign them to variables

    $imagesrc = $imageurl->src;
    $imageWidth = $imageurl->width;
    $imageHeight = $imageurl->height;
    $imageStyle = $imageurl->style;
    $imageAlt = $imageurl->alt;
    $imageBrd = $imageurl->border;
    $imageUsemap = $imageurl->usemap;

    $map = $html->find('map',0); // here we look for the map and assign it to a variable

    // here we target the map ID and Name and usemap and assign them to variables

$mapID = $map->id;
$mapName = $map->name;

    // we assigned a class to the map areas to find them easier
    $imgmaparea = $html->find('.areaSelect');

    // ok although the example only has one map area this will find multiple
    // areas and print them on the screen using the 'foreach' function 
    foreach ($imgmaparea as $value){
     $areaShape = $value->shape;
     $areaCoords = $value->coords;
     $areaHref = $value->href;
     $areaTitle = $value->title;
     $areaAlt = $value->alt;
      // print them on the screen
         echo 'Shape: '.$areaShape. '<br>';
     echo 'Coords: '.$areaCoords. '<br>';
         echo 'Href: '.$areaHref. '<br>';
         echo 'Title: '.$areaTitle. '<br>';
         echo 'Alt: '.$areaAlt. '<br>';

   }
         // print them on the screen
         echo 'name: '.$mapName. '<br>';
         echo 'Id: '.$mapID. '<br>';
         echo 'filename: '.$filename. '<br>';
         echo 'Src: '.$imagesrc. '<br>';
         echo 'Width: '.$imageWidth. '<br>';
         echo 'Height: '.$imageHeight. '<br>';
         echo 'Style: '.$imageStyle. '<br>';
         echo 'Alt: '.$imageAlt. '<br>';
         echo 'Usemap: '.$imageUsemap. '<br>';

   ?>

I have tried to explain thoroughly but if anyone has any further questions or suggestions I would be happy to here them

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文