0
<?php
    $urls = file('urls.txt');
    foreach ($urls as $url) {
        print(parse_url($url));
    }
?>

parse_url takes string as argument but not array element with type string. What should I do?

1
  • your code will throw Notice: Array to string conversion ... Commented Mar 5, 2016 at 7:52

2 Answers 2

3

There's no difference between a string and an array element of type string.

Your problem is most likely that file() by default includes in each array element the newline at the end of each line in the file. See:

http://php.net/manual/en/function.file.php

You're going to need to use FILE_IGNORE_NEW_LINES to make it not do this (see link for details)

Sign up to request clarification or add additional context in comments.

Comments

0

You could take a different approach when reading the file. Take this example:

$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);

Hope this helps you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.