英文:
Remove the first 1 and space before a phone number?
问题
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
英文:
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
答案1
得分: 1
You could match 1 followed by one (or more) horizontal whitespace characters, and assert that there is a digit following.
^1\h+(?=\d)
Or assert at 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}$)
$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
英文:
You could match 1 followed by one (or more) horizontal whitespace characters, and assert that there is a digit following.
^1\h+(?=\d)
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}$)
$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
答案2
得分: 0
To remove the first 1 and space
:
substr($string, 2)
(不使用 trim()
方法)或者如果您首先使用了 trim()
方法,则可以使用 substr($string, 1)
,但您确定您的数字将始终以 1
和 空格
开头吗?
您是否添加了任何检查或条件,以确保用户输入始终以 1
开头,然后是一个 空格
?
如果您确信如此,那么子字符串解决方案将删除这两个,并返回字符串的其余部分。
英文:
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.
答案3
得分: 0
你可以使用**正则表达式(regex)**来实现这个目标:
$phone_number = '1 800-555-1111';
echo preg_replace('/^\d\s+/', '', $phone_number);
// 将输出 800-555-1111
$phone_number = '1 800 555 1111';
echo preg_replace('/^\d\s+/', '', $phone_number);
// 将输出 800 555 1111
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论