How can one efficiently handle and manipulate data from a string output in PHP, especially in the context of game console outputs?

When dealing with string outputs from game consoles in PHP, it's important to efficiently handle and manipulate the data to extract relevant information. One way to do this is by using regular expressions to search for specific patterns or keywords within the string output. By defining and applying appropriate regex patterns, you can easily extract and process the desired data from the console output.

// Example code snippet to extract player scores from a game console output
$consoleOutput = "Player1: Score - 100, Player2: Score - 150, Player3: Score - 120";

// Define a regex pattern to match player names and scores
$pattern = '/Player(\d+): Score - (\d+)/';

// Use preg_match_all to extract player names and scores
preg_match_all($pattern, $consoleOutput, $matches, PREG_SET_ORDER);

// Iterate through the matches and display player scores
foreach ($matches as $match) {
    $player = $match[1];
    $score = $match[2];
    echo "Player $player scored $score\n";
}