警告:第6行中为foreach()提供的参数无效


Warning: Invalid argument supplied for foreach() in line 6

我试图用行号显示源代码。但我得到了

警告:为第6行上的foreach()提供的参数无效

$lines= file_get_contents("http://sitename.com");
foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />'n";
}

file_get_contents返回一个字符串,而不是可迭代的,例如,您必须首先将其转换为数组。

<?php
$lines= file_get_contents("http://sitename.com");
$lines = explode("'n", $lines);
foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />'n";
}
?>