Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1109895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:18:35+00:00 2026-05-17T02:18:35+00:00

Ok so i have a variable $phone which contains the string (407)888-9999 and I

  • 0

Ok so i have a variable $phone which contains the string (407)888-9999 and I have this code

 if($phone != ''){ $phone = "<span id='telephone'>{$phone}</span>";}
return $phone;

This works fine but now the client wants to have a some padding in between the area code and the next three numbers. So i need the code to be like this

<span id='telephone'><span class='spacer'>(407)</span>888-9999</span>";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:18:36+00:00Added an answer on May 17, 2026 at 2:18 am

    Given no one else mentioned this, I would strip out all non-numeric characters from the number. Once done you can use a regex to easily get the numbers, this way any format is valid and pretty much no matter what the user enters you can format it how you want it:

    $phone = preg_replace("~[^0-9]~", "", $phone);
    preg_match('~([0-9]{3})([0-9]{3})([0-9]{4})~', $phone, $matches);
    
    if (!empty($matches)) {
        $display = "<span id='telephone'><span class='spacer'>(" . 
                    $matches[1] . ")</span>" . $matches[2] . "-" . $matches[3] . "</span>";
    }else {
        $display = "An invalid phone number was entered.";
    }
    

    Should do it not matter how the phone number is entered as long as there are 10 digits.

    UPDATE

    You could also use the preg_replace technique with substr and forego the need for preg_match. This would actually be my preferred solution.

    $phone = preg_replace("~[^0-9]~", "", $phone);
    
    if (strlen($phone) == 10) {
        $display = "<span id='telephone'><span class='spacer'>(" . 
                    substr($phone,0,3) . ")</span>" . substr($phone,2,3) . "-" . substr($phone,5,4) . "</span>";
    }else {
        $display = "An invalid phone number was entered.";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have variable $lang which convert string to the selected language but i am
I have variable @count of datatype int.I am setting values to this @count. I
I have a variable foo that contains a time, lets say 4pm today, but
I have a variable containing a string. The string can come in several ways:
I'm using Make and I have a makefile which sets a variable with a
I have a shopping cart which at this point in time sends items that
I currently have code to share a variable between two entry points in my
I have some code which attempts to make a call within an application, but
I have a C++ program with many thousands of string literals in the code
I have the following PHP code, which is meant to insert data in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.