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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:23:13+00:00 2026-06-09T12:23:13+00:00

Someone in Stack Overflow gave me this code: $output = preg_replace( ‘/(\+?\d{2}|0)(\d{7,8})/’, ‘0$2’, $test);

  • 0

Someone in Stack Overflow gave me this code:

$output = preg_replace( '/(\+?\d{2}|0)(\d{7,8})/', '0$2', $test);

It changes user input:

+622112345 into 02112345

622112345 into 02112345

02112345 into 02112345

Unfortunately it only works for 7-8 digits. I decided to change the code into this:

$output = preg_replace( '/(\+?\d{2}|0)(\d{15})/', '0$2', $test);

By changing (\d{7,8}) into (\d{15}), I hoped I could get this RegEx to validate up to 15 digits. But, here’s what I have:

input : 083812345678910 >> output : 083812345678910 [correct]

input : 6283812345678910 >> output : 6283812345678910 [false, should be : 083812345678910]

input : +6283812345678910 >> output : 6283812345678910 [false, should be : 083812345678910]

How can I make this works on 15 digit numbers? Thanks.

UPDATE : user input can be 10, 11, 12 or even 13 digits. but not more than 15 digits. so I need this code to change the prefix : either +62, 62 or 0 INTO 0xxxx. no matter how many digits they have, 15 maximum.

  • 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-06-09T12:23:14+00:00Added an answer on June 9, 2026 at 12:23 pm

    The correct regex is:

    $output = preg_replace( '/(0|\+?\d{2})(\d{7,14})/', '0$2', $test);
    

    Even though you have 16 digit numbers, there should only be at most 14 digits after the extension, to account for the 2 digit extension. Note that this will work for more extensions other than 62, if this is not desired, replace \d{2} with 62.

    Demo

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

Sidebar

Related Questions

Hey guys, I have this great scripts someone on stack overflow helped me out
Recently someone on Stack Overflow told me that the code below does not leak,
I was just messing around to answer someone's question here on Stack Overflow, when
I've been Googling and checking on Stack Overflow and nothing has helped... hopefully someone
My colleague wrote the following stackoverflow question: other stack overflow question on this topic
This is probably the worst question I have ever posted on Stack Overflow but
I'm not sure if this an appropriate question for stack overflow. If not, I
Yesterday a nice stack overflow user helped me to trigger the Jquery Prettyphoto Lightbox
This is my first time using Stack Overflow, so if I've done something wrong
This is my first stack overflow quesiton, so if i'm not posting correctly, or

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.