How can regular expressions be effectively used in PHP to parse and extract specific values from a string?

Regular expressions can be effectively used in PHP to parse and extract specific values from a string by using functions like preg_match() or preg_match_all(). These functions allow you to define a pattern to match the desired values and extract them from the input string. By using regular expressions, you can easily extract data such as email addresses, URLs, phone numbers, or any other specific format from a given string.

$input_string = "Hello, my email is example@example.com and my phone number is 123-456-7890.";

// Extract email address
if (preg_match('/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/', $input_string, $matches)) {
    $email = $matches[0];
    echo "Email address: $email\n";
}

// Extract phone number
if (preg_match('/\d{3}-\d{3}-\d{4}/', $input_string, $matches)) {
    $phone_number = $matches[0];
    echo "Phone number: $phone_number\n";
}