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 758263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:27:32+00:00 2026-05-14T15:27:32+00:00

I have this expression: $regex_phone = ‘/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})’ .'(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})’ .'[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/’;

  • 0

I have this expression:

$regex_phone = '/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})' 
              .'(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})' 
              .'[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/';
if(!preg_match($regex_phone, $data['phone'])){ 
    $error[] = "Please enter a valid phone number."; 
}else{ 
    $data['phone'] = preg_replace($regex_phone, '($1) $2-$3 ext.$4', $data['phone']);
}

That will take a phone number such as: 803-888-8888 ext 2 as well as 803-888-8888

First number formats as:
(803) 888-8888 ext.2 — the desired effect

Second number formats as: (803) 888-8888 ext. — blank extension

How can I set it so that if $4 is blank, that ext. won’t show?

Thanks so much for any help you can offer. I hope this was clear.

  • 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-14T15:27:33+00:00Added an answer on May 14, 2026 at 3:27 pm

    I’m not sure this fancy regex is really necessary
    but lets see …

    Ahh, I modified the region around $4 in order
    to allow for missing ext numbers. Can you spot
    the differences?

    ...
    $regex_phone = 
      '/^
         (?:
           1(?:[. -])?
         )?
         (?: 
           \( (?=\d{3} \) ) # possible mistake? (probably correct)
         )?
         ([2-9]\d{2})       # $1
         (?:
           (?<=\(\d{3})\)
         )?
         \s?
         (?:
           (?<=\d{3})
           [.-]
         )?
         ([2-9]\d{2})       # $2
         [.\s-]?
         (\d{4})            # $3
         (?:
           \s? ext [.\s]? 
           (\d{1,5})?       # $4
         )?
     $/ix';
    
    if( preg_match( $regex_phone, $data['phone'], $m) ) { 
       $data['phone'] = "$m[1] $m[2]-$m[3]" . (isset($m[4]) ? " ext.$m[4]" : '');
    }
    else { 
       $error[] = "Please enter a valid phone number.<br />"; 
    }
    ...
    

    Regards

    rbo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this regular expression: ^\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$ however it is failing when i have an
I have a regular expression like this (much simplified): ^(ab)*$ And am matching against
I have quickly read over the Microsoft Lambda Expression documentation. This kind of example
I have a string containing address and a phone number (US format; (xxx) xxx-xxxx).
I have this regular expression in c#: Regex oRegex_ = new Regex(<!-- #BeginEditable \Body\[^>]*>(.*)<!--
I have a regular expression which looks like this: ^(+dd)?(?(?<=+dd)((| )(0)(| )| |)|(0))(8|ddd?)[-/ ]?dd(
I have this ReGex expression in JavaScript right now: /(.*)(,)([\]\}]+)$/.exec(stringData) . Basically it's removing
I have this expression ([^]+)|(\s\S*\s) which gets any characters that are in quotes and
I have this code in jQuery, that I want to reimplement with the prototype
I have this idea for a free backup application. The largest problem I need

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.