PHP 试图在文本文件中找到特定的行并回显它

标签 php parsing

所以我试图在一个抓取的 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/

相关文章:

php - 用 PHP 读取电子邮件

php - 用于 PHP sqlsrv_connect() 的 SQL Server 驱动程序使用忽略的错误填充 PHP 错误日志

java - 确定 PDF 页面是包含文本还是纯图片

python - 解析 html 时,为什么我有时需要 item.text 而其他人需要 item.text_content()

java - 用双引号将 CSV 行引起来/创建自定义 main 方法

regex - Bash 变量搜索和替换而不是 sed

php - 捕获 Webhook 条目并放置在新的 .csv 行上

php - 更改 MySQL WHERE 参数

php - SQL从脏数据集创建新数据库

c# - 无法解析 DateTime