不能让preg_match为网页抓取工作


Can't get preg_match working for web scraping

我这里有一些代码,但我不能让它工作。有人能帮帮我吗?

$file_string = file_get_contents('URL');
$search = "<tr><th> <a href=''>Data_name</a></th><td>(.*)</td></tr>";
preg_match($search, $file_string, $title);
$title_out = $title[1];
echo $title_out;

看看@Touki说了什么-但这可能会帮助你解决任何未来可能遇到的正则表达式问题:

  1. 你可能没有看到第一行以外的任何东西。你想使用"multiline"

  2. <tr><th> <a href=''>Data_name</a></th><td>(.*)</td></tr>看起来是非常精确的字符串。确保留白是正确的(制表符,空格,新行等)。您似乎还缺少一个超链接,不确定这是否是故意的。