PHP尝试在文本文件中查找特定行并回显它

时间:2013-04-20 16:02:29

标签: php parsing

所以我试图在一个已删除的html文件中找到一个蒸汽ID列表。这是我到目前为止,但它没有工作,它正在解析我保存为文本的html页面,并且应该输出带有下面变量的内容,并且输出一个空白页面。

   <?php
$filein = file('TF2U.txt');
foreach ($filein as $html) {
    $pattern = '#.*<a[^>]+href="steamcommunity.com/profiles/([0-9]+)/"#iA';
    $matches = NULL;
    $match_count = preg_match_all($pattern, $html, $matches);
    if ($match_count > 0) {
        echo implode($matches[1]);
        echo "<br>\n";
        }
}
?>

任何帮助都会很棒,我不确定我错过了什么,但这可能很简单。

1 个答案:

答案 0 :(得分:2)

问题是链接不是以/结尾,所以这是一个带有一些调整的解决方案:

$file = file_get_contents('TF2U.htm');
preg_match_all('#<a.*?href="(?:http://)steamcommunity.com/profiles/(?P<id>\d+)[^‌​>]+#msi', $file, $matches);
print_r($matches['id']);