0

We have lots of office locations on our website, each with a main phone number - many have been entered with the area code looking like this: (800) 555-5555

This is how I need them to all look, regardless to how it was entered: 800- 555-5555

Here's where I'm at right now

str_replace(array( '(', ')' ), '', $this->data['location_phone']);

While this removes both parenthesis, I really just need to remove the opening one and replace the closing parenthesis with a dash.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Ryan Brackett
  • 1,022
  • 11
  • 13

4 Answers4

7

Use an array for your replacements.

str_replace(array( '(', ')' ), array('', '-'), $this->data['location_phone']);

You can read more on the str_replace documentation page.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Navelpluisje
  • 286
  • 1
  • 10
1

You could do something similar to what you're already doing.. instead of replacing both ( and ) with '', you could just replace ( and then replace ) separately with -.

str_replace(array('('), '', $this->data['location_phone']);

str_replace(array(')'), '-', $this->data['location_phone']);

Or even better, combine into a single line (as indicated in other answers):

str_replace(array( '(', ')' ), array('', '-'), $this->data['location_phone']);
dub stylee
  • 3,252
  • 5
  • 38
  • 59
1

This answer seems to address the issue with preg_match & data reconstruction. But the regex posted in that answer is not that great for the kind of data cleanup described here.

So try this variation of that answer I put together which uses some great regex from a post in the official PHP documentation:

// Set test data.
$test_data = array();
$test_data[] = '1 800 555-5555';
$test_data[] = '1-800-555-5555';
$test_data[] = '800-555-5555';
$test_data[] = '(800) 555-5555';

// Set the regex.
$regex = '/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/';

// Roll through the test data & process.
foreach ($test_data as $data) {
  if (preg_match($regex, $data, $matches)) {

    // Reconstruct the number based on the captured data.
    echo "New number is: " . $matches[1] . '-' . $matches[2] . '-' . $matches[3] . '<br />';

    // Dump the matches to check what is being captured.
    echo '<pre>';
    print_r($matches);
    echo '</pre>';

  }
}

And the cleaned results—including the preg_match matches—would be:

New number is: 800-555-5555
Array
(
    [0] => 1 800 555-5555
    [1] => 800
    [2] => 555
    [3] => 5555
)
New number is: 800-555-5555
Array
(
    [0] => 1-800-555-5555
    [1] => 800
    [2] => 555
    [3] => 5555
)
New number is: 800-555-5555
Array
(
    [0] => 800-555-5555
    [1] => 800
    [2] => 555
    [3] => 5555
)
New number is: 800-555-5555
Array
(
    [0] => (800) 555-5555
    [1] => 800
    [2] => 555
    [3] => 5555
)
Community
  • 1
  • 1
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
0

Thanks. I used this for phone number to strip out all the spaces and ( ) and - so the tel://1234567890

href=tel://". str_replace(array( '(', ')',' ','-' ), array('', '','',''), $row["ContactPhone"]).">". $row["ContactPhone"]."

Win Rinkle
  • 11
  • 1