【问题标题】:Format Phone Area Code with PHP用 PHP 格式化电话区号
【发布时间】:2014-08-16 09:06:35
【问题描述】:

我们的网站上有很多办公地点,每个地点都有一个主要电话号码 - 许多都输入了类似这样的区号:(800) 555-5555

这就是我需要它们看起来的样子,不管它是如何输入的:800- 555-5555

这就是我现在的位置

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

虽然这会删除两个括号,但我真的只需要删除左括号并用破折号替换右括号。

【问题讨论】:

    标签: php str-replace reformatting


    【解决方案1】:

    谢谢。我用它作为电话号码来去掉所有的空格和 () 和 - 所以 tel://1234567890

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

    【讨论】:

      【解决方案2】:

      使用数组进行替换。

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

      您可以在str_replace 文档页面上阅读更多信息。

      【讨论】:

        【解决方案3】:

        This answer 似乎解决了preg_match 和数据重建的问题。但是该答案中发布的正则表达式对于此处描述的数据清理来说并不是那么好。

        所以试试我放在一起的这个答案的变体,它使用了来自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>';
        
          }
        }
        

        清理后的结果(包括 preg_match 匹配项)将是:

        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
        )
        

        【讨论】:

          【解决方案4】:

          您可以执行与您已经在执行的操作类似的操作.. 而不是将 ( 和 ) 都替换为 '',您只需替换 (,然后将 ) 分别替换为 @987654326 @。

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

          或者更好的是,合并成一行(如其他答案所示):

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

          【讨论】:

          • 嘿伙计,谢谢!它工作得很好。我的语法太离谱了。谢谢!!
          猜你喜欢
          • 2020-02-05
          • 1970-01-01
          • 1970-01-01
          • 2015-05-16
          • 1970-01-01
          • 1970-01-01
          • 2015-10-02
          相关资源
          最近更新 更多