2011-08-25 20 views
0

我有麻煩的屬性從地圖內。例如下面獲得使用PHP

<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來檢索頁面的其他元素XHTML地圖區域COORDS。任何幫助將不勝感激。正如我一直在繞過這個,我嘗試了使用Xpath並加載XHTML文件,但是當我嘗試獲得這些座標時,我只有42個「DOMAttr Object()」的情況,地圖上有42個區域,但.. .... 幫幫我!

+0

你需要得到COORDS的所有值,是嗎? –

+0

謝謝Tuga,但這個地方是純粹的靈感。我已經搜索了最近兩天試圖解決這個問題。當我發佈這個問題的時候,我偶然發現了一些能夠給我答案的東西。我會稍後將解決方案發布到我自己的問題上。我應該在幾年前加入這裏,這會爲我節省大量頭痛。 – apeitup

+0

解決方案是使用DomDocument? :) –

回答

0

好的,我會盡力爲任何想做類似事情的人提供儘可能多的細節。

這是一個快速示例文件。

<!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。您需要下載'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>'; 

    ?> 

我試圖徹底解釋,但如果任何人有任何問題或建議,我會很高興在這裏他們