Reputation: 45
I have Multiple URL in result.txt
file. I want to use first URL with fgets.
Problem is that fgets not passing value to file_get_html
for next process.
$file = fopen("result.txt","r");
$link = fgets($file);
// echo fgets($file); // Here I get URL successfully
$html = file_get_html($link); // Problem, Not receiving URL
foreach($html->find('.singcont a') as $a) {
$links = $a->href;
echo $links;
}
How to pass $link
value to file_get_html
?
If I use this way it works
$html = file_get_html('http://example.com/');
Upvotes: 1
Views: 801
Reputation: 47169
I'm guessing you have a newline or some other character at the end of the returned $link
string.
var_dump($link);
It might output a string that looks like this:
string(19) "http://example.com
"
To remedy the issue you could either use trim()
or substr()
:
trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] ) : string
$link = trim($link);
substr ( string $string , int $start [, int $length ] ) : string
$link = substr($link, 0, -1);
↳ PHP Manual, trim(), substr()
Upvotes: 2
Reputation: 19
You could put $html = file_get_html($link);
in the for loop, so your code will be like:
$file = fopen("result.txt","r");
$link = fgets($file);
// echo fgets($file); // Here I get URL successfully
foreach($html->find('.singcont a') as $a) {
$links = $a->href;
$html = file_get_html($link);
echo $links;
}
Upvotes: -1