How can substr() be properly used in conjunction with preg_match in PHP?
When using preg_match in PHP to extract a specific substring from a string, you can use substr() to extract the matched substring based on the preg_match result. After preg_match successfully matches the pattern in the string, you can use substr() to extract the desired substring based on the captured group index.
$string = "Hello, my email is example@example.com";
$pattern = '/([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,})/';
if (preg_match($pattern, $string, $matches)) {
$email = substr($matches[0], 0, strpos($matches[0], '@'));
echo $email; // Output: example
}