0

How do I retrieve the first line of a .CSV file as a string?

This current method I am using is failing to return the first line, but is instead returning the second line:

$content = file($filepath);
echo $content[0];
0

2 Answers 2

5

fgets() can be used to read a single line from a file.

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

Comments

3

This function will work better for you: fgetcsv()

But on a large files and if you need the performance use this function: https://www.php.net/manual/en/function.fgets.php

8 Comments

The OP wants a string, not an array.
Can I see an example of retrieving the first line as a string?
Ooops, I was wrong with the first function and you were the first with the second one :)
$handle = @fopen($filepath, "r"); if ($handle) { $line = fgets($handle); } // Your string is in the $line
What if that is returning the second line in the file? What does that mean?
|

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.