What are the potential pitfalls of using substr() to extract data from a text file in PHP?

Using substr() to extract data from a text file in PHP can be risky because it relies on fixed positions or lengths, which can lead to errors if the file structure changes. A more robust approach is to use functions like file_get_contents() or fgets() to read the file line by line and then extract the necessary data.

$file = 'data.txt';
$handle = fopen($file, 'r');

if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // Extract data from each line as needed
        $data = explode(',', $line);
        // Process the extracted data
    }

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