How can you read and output a specific string from a text file using PHP?

To read and output a specific string from a text file using PHP, you can use file_get_contents() function to read the entire file contents into a string variable. Then, you can use functions like strpos() or preg_match() to locate the specific string within the file content. Finally, you can output the specific string using echo or print statements.

$fileContent = file_get_contents('example.txt');
$specificString = 'specific string'; // specify the string you want to find

if(strpos($fileContent, $specificString) !== false) {
    echo $specificString;
} else {
    echo 'String not found in the file.';
}