2012-10-16 1 views
0

PHP 파일에서 앵커 태그를 울리는찾기 및 ... 나는 이것에 대한 많은 검색했지만 아직 작동 아무것도 발견하지 않았습니다

내가 다른 PHP 파일에있는 모든 앵커 태그를 찾아 에코 싶습니다

자신의 이름 (문서에서의 위치에 대한 href 포함).

다음

무엇을의 ...

<a name="test"></a> 

내 주요 PHP 파일에 일부 코드를 사용하여 그들 모두를 발견하고 그들 모두를 인쇄하고 싶습니다 :

예를 들어, 나는 하나 개의 파일이 있습니다 나는 시도했다 :

$dom = new DOMDocument; 
    $dom->loadHTML($content); 
    foreach($dom->getElementsByTagName('a') as $node) { 
     echo $node->getAttribute('name'); 
    } 

내가 정확하게 그것이 매우 구체적인 것은 아니지만, 감사합니다.

편집 : 현재 페이지에서 앵커를 찾아 에코하는 방법은 어떻습니까?

는 실제 사이트를 보려면 여기를 찾습니다 http://robertwbooth.co.uk

+0

에서 파일을로드 및 검색 있지만하지 않는 것 전체 DOMDocument를 물건 작업; 여러 가지 다른 방식으로로드되지만 실제로는 충분히 구체적이지 않으며 작동하지 않는 것 같습니다. –

+1

DOMDocument를로드하고 검색해야합니다. 그냥 모든 'a' 태그 요소를 http://www.php.net/manual/en/domdocument.getelementsbytagname.php로 찾아서 각 이름 속성을 확인하십시오. – peacemaker

답변

0

당신은 시도 할 수

$html = file_get_html("http://xxxxxx/support/content-entry/create-anchor-tags/"); 
$anchor = array(); 

foreach ($html->find("a") as $link) { 
    if ($link->href === false) { 
     $key = $link->id ? $link->id : ($link->name ? $link->name : false); 
     if (! $key) 
      continue; 
     if (isset($anchor[$link->id])) { 
      $anchor[$link->id] = array_merge($anchor[$link->id], array("name" => $link->name,"id" => $link->id)); 
     } else { 
      $anchor[$link->id] = array("name" => $link->name,"id" => $link->id); 
     } 
    } else { 
     if (strpos($link->href, "#") === 0) { 

      if (isset($anchor[substr($link->href, 1)])) { 
       $anchor[substr($link->href, 1)] = array_merge($anchor[substr($link->href, 1)], array("text" => $link->plaintext)); 
      } else { 
       $anchor[substr($link->href, 1)] = array("text" => $link->plaintext); 
      } 
     } 
    } 
} 

var_dump($anchor); 

출력

array 
    'id507d815fd9383' => 
    array 
     'name' => string 'id507d815fd9383' (length=15) 
     'id' => string 'id507d815fd9383' (length=15) 
     'text' => string 'Visit the Useful Tips Section' (length=29) 
    'id507d815fd93a3' => 
    array 
     'name' => string 'id507d815fd93a3' (length=15) 
     'id' => string 'id507d815fd93a3' (length=15) 
     'text' => string 'Visit the Useful Tips Section' (length=29) 
    'id507d815fd93bc' => 
    array 
     'name' => string 'id507d815fd93bc' (length=15) 
     'id' => string 'id507d815fd93bc' (length=15) 
     'text' => string 'Visit the Useful Tips Section' (length=29) 
    'id507d815fd93d3' => 
    array 
     'name' => string 'id507d815fd93d3' (length=15) 
     'id' => string 'id507d815fd93d3' (length=15) 
     'text' => string 'Visit the Useful Tips Section' (length=29) 
    'id507d815fd93eb' => 
    array 
     'name' => string 'id507d815fd93eb' (length=15) 
     'id' => string 'id507d815fd93eb' (length=15) 
     'text' => string 'Visit the Useful Tips Section' (length=29) 
    'id507d815fd9404' => 
    array 

.......... so many more 
관련 문제