所以我试图在一个抓取的 html 文件中找到一个 steam 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";
}
}
?>
任何帮助都会很棒,我不确定我遗漏了什么,但这可能很简单。
最佳答案
问题是链接没有以 /
结尾,所以这里有一些调整的解决方案:
$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']);
关于PHP 试图在文本文件中找到特定的行并回显它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16122345/