php-regexHow to format a phone number using regex in PHP?
Using regex in PHP to format a phone number is a useful way to ensure that the phone number is in the correct format. The following example code block will format a phone number in the format of (xxx) xxx-xxxx:
$phone_number = "1234567890";
$formatted_phone_number = preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone_number);
echo $formatted_phone_number;
The output of the example code will be:
(123) 456-7890
Code explanation
$phone_number = "1234567890";
: This is the original phone number that will be formatted.$formatted_phone_number = preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone_number);
: This is the regex expression that will be used to format the phone number. The expression looks for 3 digits, followed by 3 digits, followed by 4 digits. The parentheses and hyphens are added to the expression to format the phone number.echo $formatted_phone_number;
: This will output the formatted phone number.
Helpful links
More of Php Regex
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex with zero or more occurrences?
- How to match a space using PHP regex?
- How to match a single quote in PHP regex?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match special characters?
- How to use PHP regex to match whitespace?
- How to use PHP regex to match a year?
- How to use the "s" modifier in PHP regex?
- How to replace a tag using PHP regex?
See more codes...