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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:31:55+00:00 2026-05-27T13:31:55+00:00

I am trying to use an if statement to determine if a certain variable

  • 0

I am trying to use an if statement to determine if a certain variable in PHP ($phone) matches a regular expression. There are three different options – it either passes it correctly, it is a mostly correct value and just needs some slight modifications, and finally, that it is an an erroneous value and should go back to the previous page with an error message. Here’s what I’ve got so far:

EDIT: The expected value of $phone is either 123-456-7890, 1234567890, or (123) 456-7890. The desired input for the database is 123-456-7890

if (preg_match("/^\d{3}-\d{3}-\d{4}$/", $phone)) {
    // This is the 100% correct version
}
else if (preg_match("/^\d{10}$/", $phone)) {
    $phone = preg_replace("/^(.{3})/", "-", $phone);
    $phone = preg_replace("/^(.{6})/", "-", $phone);
// This is one of the mostly correct - this does not seem to work. I got a value of -37 when I tried to use it
}
else if (preg_match("/^\(\d{3}\) \d{3}-\d{4}$/", $phone)) {
    // Have not tried anything here, because the problems here are similar to the else if statement above
}
else {
    header('Location: '.$admin.'?error=phone');
// This should direct back to the previous page with an error message.
}

EDIT 2: Ok I’ve got the if and the else ifs working, however the else does not redirect back to the page or prevention insertion into the database. How do I do this?

  • 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-27T13:31:56+00:00Added an answer on May 27, 2026 at 1:31 pm

    How about:

    $phone = preg_replace("/^(\d{3})(\d{3})/", "$1-$2-", $phone);
    

    and for the second

    $phone = preg_replace("/^\((\d{3})\) /", "$1-", $phone);
    

    Explanation:

    /         : regex delimiter
    ^         : start of the string
    (\d{3})   : group 1 capture 3 digits
    (\d{3})   : group 2 capture 3 next digits
    /
    

    replacement part :

    $1        : value captured in group 1 (the first 3 digits)
    -         : a dash
    $2        : value captured in group 2 (the next 3 digits)
    -         : a dash
    

    For dealing with all test cases, I’d do:

    // Remove all non digit
    $phone = preg_replace('/\D+/', '', $phone);
    // then we must have a 10 digit number
    if (preg_match('/^\d{10}$/', $phone) {
        $phone = preg_replace("/^(\d{3})(\d{3})/", "$1-$2-", $phone);
    } else {
        // error
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the if statement in flash to check if a variable
I'm trying to use a break statement in a for loop, but since I'm
I'm using Python 2.5. I'm trying to use this 'with' statement. from __future__ import
I'm trying to use a BULK INSERT statement to populate a large (17-million-row) table
I am trying to use Eval inside a IF Statement and Repeater. I want
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I'm trying to determine CPU usage of my PHP scripts. I just found this
for some of my php pages, i use the $_SESSION to determine if a
Trying to use a case statement to control the operation of limit. Sample query
I'm trying to use a SELECT statement and fetch all rows from a table

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.