简体   繁体   中英

PHP Get name of a specific html element via get_file_contents

I retrieve html of an external page with get_file_contents

In this html code i only need to get the "name" of the 4 firts <a> elements that actualy have a "name".

<a> element look like this :

<a name="1991562"></a>

And i want to display name of 4 firts <a> element like this: 1991562,1991563,1991564,1991565

Is there a way to do that easily with php ?

<?php

$doc = new DOMDocument;
$doc->loadHTML('<html><body>Test<br><a name="112">test</a>  <a>test 2</a></body></html>');

$a = $doc->getElementsByTagName('a');
foreach ($a as $link) {
   $name = $link->getAttribute('name');
   if($name && is_numeric($name)){
    echo $name;
   }
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM