[英]issue using file_get_contents with a variable as the link to target file
我正在尝试遍历一系列链接,并使用file_get_contents获取源代码并从中获取某些内容:
$links = file('mysite2.txt');
foreach($links as $link) {
$f = file_get_contents("$link");
$source = $f;
if(preg_match('/<meta pro=\"(.*)\" \/>/',$source,$matches)) {
$answer = $matches[1];
echo "$answer";
}
}
现在,当我在file_get_contents
( file_get_contents("$link")
)函数中使用$ link时, preg_match
条件为false。 然而,当我在file_get_contents
(`file_get_contents(“http://www.site.com/something”)中使用my_site2.txt中的一个链接时,它可以正常工作。
我甚至尝试使用另一个只包含一个链接的txt文件,该链接在源代码中具有正确的字符串。
我也试过没有引号: file_get_contents($link)
有几件事情。
$f
文件名将包含换行符。 您需要为file
添加其他标志以防止这种情况:
$links = file('mysite2.txt', FILE_IGNORE_NEW_LINES);
你不需要在正则表达式中转义双引号。
你确定你的正则表达式在/>
之前需要一个空格吗? 你的正则表达式将匹配<meta pro="test" />
但不符合<meta pro="test"/>
。
在foreach循环中进行decalred时$ link的内容可能是一个数组或一个对象。
在$ link上尝试var_dump()并查看其中的内容,这可能有助于您了解如何操作它的内容。
你是怎么设置.txt文件的?
这是一个解决方案: -
如果你只是像这样的链接
链接1,链接2,LINK3,LINK4,link5
然后做
$links = explode( ',' , file_get_content($file) ) ;
然后foreach这个数组
只需从$f = file_get_contents("$link");
删除引号$f = file_get_contents("$link");
使$f = file_get_contents($link);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.