0

I am reading a long string coming from an editor using PHP. It misses the first line. Please see the code below.

$separator = "\r\n";
$line = strtok($article, $separator);

while ($line !== false) {
    # do something with $line
    $line = strtok( $separator );
    echo $line;
}

If the text is like -

Line1Line1Line1Line1 Line1Line1Line1
Line2Line2Line2Line2 Line2Line2Line2
Line3Line3Line3 Line3Line3

Then it prints from the second one.

Any help is highly appreciated.

3
  • 2
    Because you already consumed one line before the loop. $line = strtok($article, $separator); will have the first line. Commented May 1, 2021 at 15:41
  • Also, that's a quite convoluted workaround for reading a file line-wise. Commented May 1, 2021 at 15:46
  • @El_Vanja Thanks for the hint. Understood. Commented May 1, 2021 at 19:50

1 Answer 1

1

Maybe something like this could work...

$separator = "\r\n";
$line = strtok($article, $separator);

while ($line !== false) {
    echo $line;
    $line = strtok( $separator );
    
}
Sign up to request clarification or add additional context in comments.

2 Comments

While this may solve the problem, it would be good to include an explanation so that the OP and any future readers can learn from your answer.
Thank you. I have understood now.

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.