删除除数字,字母x和加号(正则表达式,PHP)之外的所有内容

时间:2022-09-08 02:20:22

Many variations of this question exist, and I tried a few generators, but I can't seem to find a regular expression that matches this. Say I have a phone number that looks like "345-324-3243 X039" I want to remove everything but the digits, the letter x and the plus sign (appears in international numbers).

存在这个问题的许多变体,我尝试了一些生成器,但我似乎找不到与之匹配的正则表达式。假设我的电话号码看起来像“345-324-3243 X039”我想删除除数字,字母x和加号(显示在国际号码中)之外的所有内容。

Here is my current non-working regex and code:

这是我目前的非工作正则表达式和代码:

$phone = "345-324-3243 X039";
preg_replace('[^\d|x|\+]', '', $phone);

I want it to come out to "3453243243X039" but instead I get "30234-2349".

我希望它出来“3453243243X039”,但我得到“30234-2349”。

3 个解决方案

#1


7  

Lose the | as it is a literal inside a character class, make sure to capture an uppercase "X" (along with a lowercase "x"), use the regex "/quotes/" (otherwise it treats is as "[quotes]") and use the result:

失去了|因为它是字符类中的文字,请确保捕获大写的“X”(以及小写的“x”),使用正则表达式“/ quotes /”(否则它将其视为“[quotes]”)和使用结果:

$phone = "345-324-3243 X039";
$phone = preg_replace('/[^\dxX+]/', '', $phone);
// $phone is 3453243243X039

Happy coding.

#2


1  

Remove the |s from your regex and escape the +.

从正则表达式中删除|并转义+。

#3


1  

So you're not only getting residual - characters in your results, but entirely different numbers? That's bizarre to me. You can't get 30234-2349 from 345-324-3243 X039 just by deleting characters; the only 0 in that input is right near the end. It's almost like characters are getting shuffled around as well as deleted.

所以你不仅会在结果中得到残差字符,而且数字完全不同?这对我来说很奇怪。你无法通过删除字符从345-324-3243 X039获得30234-2349;该输入中唯一的0接近结尾。这几乎就像角色被拖曳以及被删除一样。

I do notice you don't have regex delimiters around your pattern. I don't even know what the effect of that would be, since I don't recall ever trying it, but that's the only functional problem in your regex. The |s inside your character class will be interpreted as literal | characters, but that shouldn't affect the results for the sample input you posted.

我注意到你的模式周围没有正则表达式分隔符。我甚至不知道那会产生什么影响,因为我不记得曾经尝试过,但这是你的正则表达式中唯一的功能问题。字符类中的| s将被解释为literal |字符,但不应影响您发布的示例输入的结果。

The code I'd use is this:

我使用的代码是这样的:

$phone = preg_replace('/[^\dx+]/i', '', $phone);

I've wrapped the regex in /.../ delimiters, removed the |, unescaped the + (shouldn't have any effect inside a character class), and made it case-insensitive to catch the capital X.

我已将正则表达式包装在/.../分隔符中,删除了|,未转义的+(在字符类中不应该有任何效果),并使其不区分大小写以捕获大写字母X.

#1


7  

Lose the | as it is a literal inside a character class, make sure to capture an uppercase "X" (along with a lowercase "x"), use the regex "/quotes/" (otherwise it treats is as "[quotes]") and use the result:

失去了|因为它是字符类中的文字,请确保捕获大写的“X”(以及小写的“x”),使用正则表达式“/ quotes /”(否则它将其视为“[quotes]”)和使用结果:

$phone = "345-324-3243 X039";
$phone = preg_replace('/[^\dxX+]/', '', $phone);
// $phone is 3453243243X039

Happy coding.

#2


1  

Remove the |s from your regex and escape the +.

从正则表达式中删除|并转义+。

#3


1  

So you're not only getting residual - characters in your results, but entirely different numbers? That's bizarre to me. You can't get 30234-2349 from 345-324-3243 X039 just by deleting characters; the only 0 in that input is right near the end. It's almost like characters are getting shuffled around as well as deleted.

所以你不仅会在结果中得到残差字符,而且数字完全不同?这对我来说很奇怪。你无法通过删除字符从345-324-3243 X039获得30234-2349;该输入中唯一的0接近结尾。这几乎就像角色被拖曳以及被删除一样。

I do notice you don't have regex delimiters around your pattern. I don't even know what the effect of that would be, since I don't recall ever trying it, but that's the only functional problem in your regex. The |s inside your character class will be interpreted as literal | characters, but that shouldn't affect the results for the sample input you posted.

我注意到你的模式周围没有正则表达式分隔符。我甚至不知道那会产生什么影响,因为我不记得曾经尝试过,但这是你的正则表达式中唯一的功能问题。字符类中的| s将被解释为literal |字符,但不应影响您发布的示例输入的结果。

The code I'd use is this:

我使用的代码是这样的:

$phone = preg_replace('/[^\dx+]/i', '', $phone);

I've wrapped the regex in /.../ delimiters, removed the |, unescaped the + (shouldn't have any effect inside a character class), and made it case-insensitive to catch the capital X.

我已将正则表达式包装在/.../分隔符中,删除了|,未转义的+(在字符类中不应该有任何效果),并使其不区分大小写以捕获大写字母X.