使用php获取锚标记名称值

时间:2022-06-01 20:38:01

i am getting a page with php function like following

我正在获得一个PHP功能的页面,如下所示

<?php $html = file_get_contents('http://www.domain.com');?>

i want to get anchor tag name field value using php. that is like

我想使用PHP获取锚标签名称字段值。就像

<a href="something" name="something">some value</a>

I don't know how to do it. found nothing in google it.

我不知道怎么做。在google中找不到任何东西。

1 个解决方案

#1


0  

This should get you going if you need to use PHP. But maybe you want to work in JS in the client? The code is commented. Obviously you need to get to BenM's link.
z1.htm:

如果您需要使用PHP,这应该可以帮到您。但也许你想在客户端的JS中工作?代码被评论。显然你需要得到BenM的链接。 z1.htm:

<html><head></head><body>
<a href="something" name="something">some value</a>
<a href="something2" name="something2">some value2</a>
<a href="something3" name="something3">some value3</a>
</body></html>

z1.php:

<?php
$sfile = file_get_contents('z1.htm'); // loads file to string
$html = new DOMDocument; // is object class DOMDocument
$html->loadHTML($sfile); // loads html
$nodelist = $html->getElementsByTagName('a'); // nodes
foreach ($nodelist as $node) {
  echo $node->nodeValue, "<br />\n"; }
?>

#1


0  

This should get you going if you need to use PHP. But maybe you want to work in JS in the client? The code is commented. Obviously you need to get to BenM's link.
z1.htm:

如果您需要使用PHP,这应该可以帮到您。但也许你想在客户端的JS中工作?代码被评论。显然你需要得到BenM的链接。 z1.htm:

<html><head></head><body>
<a href="something" name="something">some value</a>
<a href="something2" name="something2">some value2</a>
<a href="something3" name="something3">some value3</a>
</body></html>

z1.php:

<?php
$sfile = file_get_contents('z1.htm'); // loads file to string
$html = new DOMDocument; // is object class DOMDocument
$html->loadHTML($sfile); // loads html
$nodelist = $html->getElementsByTagName('a'); // nodes
foreach ($nodelist as $node) {
  echo $node->nodeValue, "<br />\n"; }
?>