【问题标题】:Finding and echoing anchor tags in a php file在 php 文件中查找和回显锚标记
【发布时间】:2012-10-16 15:18:33
【问题描述】:

我为此搜索了很多,但还没有找到任何有用的东西......

我想在另一个 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

【问题讨论】:

  • 整个 DOMdocument 东西加载文件并搜索,但它似乎不起作用;很多不同的方式,但没有一个是真正足够具体的和/或似乎不起作用。
  • 加载 DOMDocument 和搜索应该可以工作。只需找到所有带有php.net/manual/en/domdocument.getelementsbytagname.phpa 标签元素,然后检查它们的每个名称属性

标签: php tags find anchor echo


【解决方案1】:

你可以试试

$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

【讨论】:

    猜你喜欢
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    相关资源
    最近更新 更多