Stripping a phone number of its parenthesis, spaces, and hyphens in PHP?

php format phone number with dashes
python format phone number with dashes
php remove all characters except numbers
preg_replace
php phone formatting
laravel format phone number
phone number pattern in php
remove country code from phone number php

I have a phone number stored in $phone, it looks like this: (555) 555-5555. I want it to look like this: 5555555555. How do I take the string and strip it of hyphens, spaces, and parenthesis?

With a regexp. Specifically, use the preg_replace function:

$phone = preg_replace('/\D+/', '', $phone);

Practical PHP and MySQL Website Databases: A Simplified Approach, Stripping a phone number of its parenthesis, spaces, and hyphens in PHP? 1005 . I have a phone number stored in $phone , it looks like this: (555) 555-5555 . I have a phone number stored in $phone, it looks like this: (555) 555-5555. I want it to look like this: 5555555555. How do I take the string and strip it of hyphens

preg_replace("/[^0-9]/", "", $phone);

Removing parenthesis from phone numbers and adding - php, This means β€œIf the password has NOT been entered, put an error message into mysqli_real_escape_string($dbcon, trim($psword1)); }else{ $errors[] = 'Your two Phone number entries may contain numbers, spaces, brackets, and hyphens. I have AplphaNumeric part numbers that sometimes contain hyphens or decimals. In order to make them more searchable (since people don't always use the hyphens or decimals properly when they search) I would like to create all the possible variants of the part number, but with one formula. So AHW18.787 becomes AHW18 787 and AHW18787 with

Cumbersome method for regex avoiders:

implode(array_filter(str_split('(555) 555-5555'), 'is_numeric'));

trim - Manual, Here is what the code looks like: $this-> phoneNumber = $phoneNumber; area code and the next block of numbers) with a space instead of a hyphen, likeΒ  I am unable to strip one space before and after a hyphen. I have tried: - sample.replaceAll("[\\s\\-\\s]","") and permutations to no avail. I dont want to strip all spaces, neither all the intervening spaces. I am trying to parse a string based on " "but want to eliminate "-". Any insight appreciated.

Method 1

Another option is to simply use the preg_match_all with a simple expression:

[0-9]+
Test
$phone_number_inputs = ['(555) 555-5555', '(444) 444 4444', '333-333-3333', '1-222-222-2222', '1 (888) 888-8888', '1 (777) 777-7777',
    '11111(555) 555-5555'];

$phone_number_outputs = [];
foreach ($phone_number_inputs as $str) {
    preg_match_all('/[0-9]+/', $str, $numbers, PREG_SET_ORDER, 0);

    foreach ($numbers as $number) {
        $phone_number .= $number[0];
    }

    if (strlen($phone_number) == 10 || strlen($phone_number) == 11) {
        array_push($phone_number_outputs, $phone_number);
        print("🍏 " . $phone_number . " may be a phone number! \n");
    } else {
        print("🍎 " . $phone_number . " may not be a phone number! \n");
    }
    $phone_number = null;
}

var_export($phone_number_outputs);
Output
🍏 5555555555 may be a phone number! 
🍏 4444444444 may be a phone number! 
🍏 3333333333 may be a phone number! 
🍏 12222222222 may be a phone number! 
🍏 18888888888 may be a phone number! 
🍏 17777777777 may be a phone number! 
🍎 111115555555555 may not be a phone number! 

and var_export($phone_number_outputs); would print:

array (
  0 => '5555555555',
  1 => '4444444444',
  2 => '3333333333',
  3 => '12222222222',
  4 => '18888888888',
  5 => '17777777777',
)
Method 2

It would be best to find solutions similar to method 1.

However, we can also design some more complicated expressions depending on the inputs that we might have, such as with:

^(?:[0-9][- ])?\(?[0-9]{3}\)?[- ][0-9]{3}[- ][0-9]{4}$

If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


RegEx Circuit

jex.im visualizes regular expressions:

Strip characters, Including spaces, for tel: link, trim β€” Strip whitespace (or other characters) from the beginning and end of a string This function returns a string with whitespace stripped from the beginning and end $start = microtime(true); while($times > $count){ eval($function); $ count++; And also prevents the open-square-bracket from being seen as part of theΒ  matches numbers that start with a plus sign, they may contain hyphens, parenthesis and spaces. but no plus signs other than at the start. and also no characters. also removes hyphens, spaces and parenthesis.

Remove all characters except numbers from a phone number and , If you're trying to escape a phone number link, just using esc_url() with the tel: included should be sufficient: esc_url( 'tel:' . $member_phone );. I am creating a phone number search for this app and need to be able to pull back all records where the phone number searched for matches a phone number in the table. The problem is, however, that the phone number in the database may have extra characters such as spaces, parentheses, hyphens, etc.

PHP: Remove special characters from a string., PHP, The Web. <?php. $number = β€œ(212)222.1111”;. //Strip Out Everything Except Numbers //Add Dashes Tags: format phone numberΒ  I would like to create an update query that removes all hyphens "-" and parenthesis "(" ")" from fields containing phone numbers. Its okay if I have to remove all hyphens first, then all left parenthesis, then all left parenthesis. I would rather do it one-by-one. Also, I would prefer to do this in the query design builder as opposed to SQL.

Remove parentheses, dashes, and spaces from phone number, In this post, I will provide a number of different examples on how to strip out non- alphanumeric characters. This can be a The PHP string above contains an asterisk, a hyphen, an apostrophe, a blank space and two curly brackets. However Remove everything except letters, numbers and white spaces. If you want toΒ  Most of the time, I like to use parenthesis and hyphens in phone numbers. Using periods can make the phone number look like a web address, and all hyphens can, at first glance, look a bit like a social security number. β€œ(432) 555-1234” looks like a phone number, and nothing else, rather than β€œ432-555-1234” or β€œ432.555.1334.”

Comments
  • Awesome. Thanks.. :)
  • perfect - cleans '(555).555-55.55' to '5555555555'
  • Thank you. Super helpful.