Are there any best practices or recommendations for reading lines from a text file in PHP to avoid counting extra characters?

When reading lines from a text file in PHP, it's important to be mindful of extra characters such as newline characters (\n) at the end of each line. To avoid counting these extra characters, you can use the rtrim() function to remove any trailing whitespace characters from each line before processing it.

$file = fopen("example.txt", "r");

if ($file) {
    while (($line = fgets($file)) !== false) {
        $line = rtrim($line); // Remove trailing whitespace characters
        // Process the line here
    }

    fclose($file);
} else {
    echo "Error opening file.";
}