How can one modify a regular expression pattern in PHP to extract only the first occurrence of a specific text pattern, such as the text before the first slash?

To extract only the first occurrence of a specific text pattern in PHP, such as the text before the first slash, you can modify the regular expression pattern to capture the desired text. You can use the preg_match function in PHP to search for the pattern in the input string and extract the matched text. By using the caret symbol (^) to anchor the search at the beginning of the string and using a capturing group to extract the text before the first slash, you can achieve the desired result.

$input = "example/text/after";
$pattern = '/^([^\/]+)/'; // Match text before the first slash
preg_match($pattern, $input, $matches);
$firstOccurrence = $matches[1];

echo $firstOccurrence; // Output: example