【问题标题】:PHP file_get_contents() and query stringPHP file_get_contents() 和查询字符串
【发布时间】:2011-05-24 21:02:04
【问题描述】:

我有一个问题 - 一个无法解析的解析器。这没用!它不回馈任何东西!好吧,我想取回一些东西 - 并将结果存储在 mysql 数据库中。

<?PHP
// Original PHP code by Chirp Internet: http://www.chirp.com.au
// Please acknowledge use of this code by including this header.

$url = "http://www.edi.admin.ch/esv/00475/00698/index.html?lang=de";

//$input = @file_get_contents($url) or die("Could not access file: $url");

$input = file_get_contents($url) or die("Could not access file: $url"); 

$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER))
{
    foreach($matches as $match) 
    {
        // $match[2] = all the data i want to collect...
        // $match[3] = text that i need to collect - see a detail-page
    }
}
?>

这有点超出我的想象:它没有返回任何结果。我必须将file_get_contents() 与查询字符串一起使用吗?

【问题讨论】:

  • 你确定你使用 file_get_contents 从 url 中得到了一些东西吗?
  • 请说出它的作用。不工作不是问题。
  • 用正则表达式解析HTML请参考this答案。
  • 你真的知道“查询字符串”是什么意思吗?还有为什么你之前关于同一主题的问题会出现回归(正则表达式提取而不是实际的 dom 解析)?
  • 您假设file_get_contents() 可以访问远程文件,但事实上,它并不总是这样做。这取决于特定设置,出于安全原因,该设置大部分时间都处于关闭状态。

标签: php mysql parsing file curl


【解决方案1】:

在这里工作正常:

$url = "http://www.edi.admin.ch/esv/00475/00698/index.html?lang=de";

$doc = new DOMDocument();
// Supress warnings for screwy HTML
@$doc->loadHTMLFile($url);

// Use DOM functionality to get all links
$link_list = $doc->getElementsByTagName('a');

$links = array(); 
foreach($link_list as $link) {
  if($link->getAttribute('href')) {
    // and put their href attributes and
    // text content in an array
    $link_info['href'] = $link->getAttribute('href');
    $link_info['text'] = $link->nodeValue;
    $links[] = $link_info;
  }
}

print_r($links);

输出:

Array
(
    [0] => Array
        (
            [href] => #webNavigationDiv
            [text] => Direkt zur Navigation [Alt + 1]
        )

    [1] => Array
        (
            [href] => #contentStart
            [text] => Direkt zum Inhalt [Alt + 2] 
        )

    [2] => Array
        (
            [href] => #keywords_fast
            [text] => Direkt zur Suche [Alt + 5]
        )

【讨论】:

    【解决方案2】:

    你正在做一些你不应该做的事情——用正则表达式解析 HTML。不要这样做!

    改用 DOM 解析函数。 PHP 的 DOMDocument 类非常易于使用,并且比正则表达式更易读(和稳定):

    $dom = new DOMDocument;
    $dom->loadHTML($yourHTML);
    
    $links = $dom->getElementsByTagName('a');
    
    $hrefs = array();
    foreach ($links as $link) {
        $hrefs[] = $link->getAttribute('href');
    }
    

    如果您愿意,获取其他数据(例如文本内容或其他属性名称)非常简单。

    【讨论】:

      【解决方案3】:

      如果启用了适当的 fopen 包装器,您只能使用带有 url 的类似 fopen 的函数。

      见:http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

      虽然我认为“正则表达式不适用于 html”,但如果这只是一个小脚本,谁在乎呢?话虽这么说,DOMDocument 和朋友都非常容易使用。

      乔什

      【讨论】:

        猜你喜欢
        • 2010-12-31
        • 1970-01-01
        • 2023-03-23
        • 2021-01-15
        • 1970-01-01
        • 1970-01-01
        • 2015-01-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多