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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:58:52+00:00 2026-06-14T02:58:52+00:00

I want to replace all non-numeric characters in a string except a + .

  • 0

I want to replace all non-numeric characters in a string except a +.

I’ve tried this question, but it’s not working…

Here is what I have at the moment:

$nn = preg_replace("/([^0-9\/+]+)/", "", $string );

It works 100%, except for removing the + in any way…

EDIT

My input will always contain only 1 +, and should there be more, they should be removed.

Basically, if a user enters a phone number as (015) 234-2634 it should be returned as +27152342634 (South African Country Code – I add the +27 at a later stage) But if +27 (15) 234-2634 is entered, +27152342634 should be returned.

  • 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-14T02:58:54+00:00Added an answer on June 14, 2026 at 2:58 am

    You should be able to do it with the following regex:

    [^0-9+]
    

    In preg_replace():

    $nn = preg_replace('/[^0-9+]/', '', $string);
    

    Your current regex also keeps a forward-slash, so to keep that functionality:

    $nn = preg_replace('/[^0-9\/+]/', '', $string);
    

    Sample code with output:

    <?php
    $string = '+27 (15) 234-2634';
    $nn = preg_replace("/[^0-9+]/", "", $string );
    echo $nn . "\n";
    ?>
    

    Results in:

    +27152342634
    

    UPDATE (keep only first matching +)
    Per your latest question-update, you also only want to keep the first + symbol found. To do this, since there may not be a “rule” regarding the location of the first symbol (such as “it has to be the first character in the string), I would suggest using additional methods other than just preg_replace():

    $nn = preg_replace("/[^0-9+]/", "", $string);
    if (substr_count($nn, '+') > 1) {
        $firstPlus = strpos($nn, '+') + 1;
        $nn = substr($nn, 0, $firstPlus) . str_replace('+', '', substr($nn, $firstPlus));
    }
    

    This code will perform the original preg_replace() as normal and then, if there are more than 1 + symbols in the result, it will get a sub-string of the result up to the first +, then perform a string-replacement to replace all remaining + symbols. You could always use a second preg_replace() here too, but to remove only a + symbol it would be overkill.

    Here’s a codepad entry for the sample.

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

Sidebar

Related Questions

I want to replace all non-alpha characters in a string with a plus '+'
I want to replace all non-numerical characters in a VARCHAR field in MySQL, because
I want to replace all "%" with "%%" in my string. However, my string
I want to replace all unnecessary breaks in a string. I have written a
I want to change image names on below conditions All non word characters turns
I want to remove all strange characters from a string to make it url
I want to replace non-ascii characters (for now, only spanish), by their ascii equivalent.
All I want is to replace several words to other words. I tried the
how can i to remove all characters non-language ? i want to remove characters
I want sed to omit all non-matching lines, and only output the replaced string

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.