How can PHP functions like fgets and substr be used to manipulate and extract data efficiently from a text file?

To manipulate and extract data efficiently from a text file using PHP functions like fgets and substr, you can read the file line by line with fgets and then use substr to extract specific portions of the text. By combining these functions, you can efficiently process and extract data from the text file.

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

if ($file) {
    while (($line = fgets($file)) !== false) {
        $extractedData = substr($line, 0, 10); // Extract the first 10 characters from each line
        echo $extractedData . "\n";
    }

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