1

I did searches and figured this would be an easy find. Though I haven't found anything related to exactly what I need and similar questions are from people from overseas that have different phone formats with plus signs. A lot of my phone number entries start with a 1 and a space. I did a simple str_replace then I realized if a number has a 1 in the middle followed by a space, then that would mess it up

$string = trim($string);

$string = str_replace('1 ', '', $string); 

That works for phone numbers beginning with

1 800-555-1111

1 222-555-1111

But in the case the phone number is

1 222 551 1111

Then that 551 and space would mess things up. How can it be rewitten so that only the first 1 and the following space is removed and the rest is untouched? Thanks.

EDIT

I forgot to mention that the entries do not always start with a 1 and a space. Sometimes they may be

888-555-1111

888 555 1111

1 888 555 1111

4
  • 1
    Maybe it would be best to use a regex Commented May 14, 2023 at 2:06
  • Thanks but someone could pay me a million dollars and hold a gun to my head and give me 10 years to figure out regex/preg type stuff and I would never figure it out. Even the tutorials give the bare basics and never explain the difficult parts that a human can understand. Commented May 14, 2023 at 3:19
  • @I_hate_arrays42 I have updated in the answer, try that Commented May 14, 2023 at 4:00
  • Are you looking for this? ^1\h+ regex101.com/r/lTlD8A/1 Commented May 14, 2023 at 12:35

3 Answers 3

1

You could match 1 followed by one (or more) horizontal whitespace characters, and assert that there is a digit following.

^1\h+(?=\d)

Regex demo

Or assert least 10 digits to the right where there can be an optional character other than a digit or a newline in between.

^1\h+(?=\d(?:[^\d\n\r]?\d){9}$)

Regex demo } Php demo

$strings = [
    "1 800-555-1111",
    "1 222-555-1111",
    "888-555-1111",
    "1 888 555 1111",
    "1 ",
    "1 2"
];

$pattern = '/^1\h+(?=\d(?:[^\d\n\r]?\d){9}$)/';

foreach ($strings as $s) {
    echo preg_replace($pattern, '', $s) . PHP_EOL;
}

Output

800-555-1111
222-555-1111
888-555-1111
888 555 1111
1 
1 2
Sign up to request clarification or add additional context in comments.

Comments

0

You can use regex to achieve this:-

$phone_number = '1 800-555-1111'
echo preg_replace('/^\d\s+/', '', $phone_number);
// Will echo 800-555-1111

$phone_number = '1 800 555 1111'
echo preg_replace('/^\d\s+/', '', $phone_number);
// Will echo 800 555 1111

1 Comment

Thanks. I tried your code first and it works.
0

To remove the first 1 and space

substr($string,2)(without using the trim() method) or substr($string,1)(if you used the trim() method first) will work but are you sure that your number will always start with a 1 and whitespace?

Do you have any checks or conditions added to make sure that the user input will always start with a 1 and followed by a space?

If you are sure about that, then the substring solution removes those first two and then returns the rest of the string.

2 Comments

Oh I forgot to mention that is part of the problem. It doesn't always start with a 1 and a space which is why I didn't use substr(). Sometimes the number could be 888-555-1111
I don't think that was your main issue though because even the answer you accepted does not deal with a situation where someone provides a number like this 1800 555 1111 for example. The 1 will still be there because the regex is looking for a 1 that's followed by a whitespace: But I'm glad if it seems to work out for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.