0
<?php
$urls = file('urls.txt');
foreach ($urls as $url) {
print(parse_url($url));
}
?>
parse_url将字符串作为参数,但不包含数组元素与字符串类型。我该怎么办?PHP parse_url不适用于数组元素
<?php
$urls = file('urls.txt');
foreach ($urls as $url) {
print(parse_url($url));
}
?>
parse_url将字符串作为参数,但不包含数组元素与字符串类型。我该怎么办?PHP parse_url不适用于数组元素
字符串与字符串类型的数组元素没有区别。
您的问题很有可能是file()
默认在每个数组元素中包含文件中每行末尾的换行符。请参阅:
http://php.net/manual/en/function.file.php
你会需要使用FILE_IGNORE_NEW_LINES
,使其无法做到这一点(见链接了解详细信息)
读取文件时,您可以采取不同的方法。拿这个例子:
$fp = fopen('urls.txt', 'r');
while(($buffer = fgets($fp, 1024)) != NULL){
//where 1024 is maximum length of each line in file
if(gettype($buffer) == 'string'){
echo "$buffer\n";
}
}
fclose($fp);
希望这可以帮助你。
你的代码会抛出'Notice:Array to string conversion ...' – RomanPerekhrest