I’m trying to loop through an array of links, and use the file_get_contents to get the source code and take certain content from it:
$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";
}
}
Now when i use $link in the file_get_contents (file_get_contents("$link")) function, the preg_match condition is false. Yet when i use one of the links in my_site2.txt in the file_get_contents (`file_get_contents(“http://www.site.com/something”)) it works fine.
I’ve even tried using a different txt file which only contains one link, which had the correct string in the source code.
Iv also tried without quotes: file_get_contents($link)
There were a couple of things.
filenames in
$fwill contain newlines. You need to add an additional flag forfileto prevent this:you don’t need to escape double quotes in your regex.
are you sure that your regex needs a space before
/>? Your regex will match<meta pro="test" />but not<meta pro="test"/>.