What is the best way to read and extract specific data from a file in PHP?

When reading and extracting specific data from a file in PHP, the best way is to use file handling functions like fopen, fread, and fclose to read the file line by line. You can then use regular expressions or string manipulation functions to extract the specific data you need from each line.

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

if ($file) {
    while (($line = fgets($file)) !== false) {
        // Extract specific data from $line using regex or string functions
        // Example: 
        if (preg_match('/^Name: (.*)$/', $line, $matches)) {
            $name = $matches[1];
            echo $name . "\n";
        }
    }

    fclose($file);
} else {
    echo "Unable to open file.";
}
?>