How to read a text file and search for a certain string before a colon and then show the content after the colon?

I have a file that contains something like this:


I would like to be able to search the file for say test and it set the string after the : to a variable so it can be displayed, used etc.

Here is the code I have so far for finding ‘test’ in the file.

$file = 'file.txt';
$string = 'test';

$searchFile = file_get_contents($file);
if (preg_match('/b'.$string.'b/', $searchFile)) {
    echo 'true';
    // Find String
} else {
    echo 'false';

How would I go about doing this?

Source: regex

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.