从当前页面获取特定的href

时间:2012-10-16 20:11:59

标签: php dom

尝试从当前页面中提取href路径(#navbar a href,以$ pg结尾)。

在第一个foreach上发出警告:

file_get_contents() [function.file-get-contents]: Filename cannot be empty.

我该如何解决这个问题?

<?php
include_once 'path/to/simple_html_dom.php';

$pg = 'c.html';
echo 'Page: ' . $pg . "<br />"; // Correct: c.html
$cpath = $_SERVER['REQUEST_URI'];
echo var_dump($cpath).": Current Path"."<br />";  // Correct: /copyr/index.php
$cfile = basename($cpath);
echo 'Current File: ' . $cfile . "<br />";  // Correct: index.php
$html = file_get_html($cfile);

foreach($html->find(sprintf('#navbar a[href=%s]', $pg)) as $path) {  // BROKEN
    echo 'Path: ' . $path."<br />";
    $tpath = $path."/".$pg;
    echo 'Total Path: ' . $tpath;
    $html->clear();
}

$html = file_get_html($tpath);

foreach($html->find('#testdiv2') as $ret) {
  echo $ret;
  $html->clear();
}

?>

1 个答案:

答案 0 :(得分:2)

basename返回路径的最后一部分。如果REQUEST_URI/结尾(如http://yourdomain.arg/,则basename返回空字符串,而file_get_html则需要uri或文件名。