In the context of PHP forum posts, how can preg_match_all be utilized to handle multiple instances of a specific pattern, such as usernames preceded by "@" symbols?

When handling multiple instances of a specific pattern, such as usernames preceded by "@" symbols in PHP forum posts, you can use the preg_match_all function to extract all occurrences of the pattern from the input text. This function allows you to specify a regular expression pattern to match against the input text and retrieve all matches in an array. By using preg_match_all, you can efficiently extract all usernames preceded by "@" symbols in a given forum post.

$input_text = "Hey @user1, have you seen @user2's latest post? @user3 is also active on the forum.";
$pattern = '/@(\w+)/';
preg_match_all($pattern, $input_text, $matches);

$usernames = $matches[1];

foreach ($usernames as $username) {
    echo $username . "\n";
}