PHP只删除文本中的电话号码(PHP remove only and only phone numbers from text)

这是一个包含数字和电话号码的示例文本/字符串:

哦,天哪,我今天有1000件事要做,我想完成阅读20,000海底联赛,1954年的书,但是,嘿,我会在下午4点到8点见到你,我的单元格是374-7657654,或者你可以给我打电话( 374)-334-5674然后我可以从16:30到19:45(在欧洲时间!)

我如何删除/检测只有电话号码(以任何格式编写,因为我无法预见用户会如何编写它们?

我试过了

(\(?+[0-9]{3,15}+[\- ]?+[0-9]?+\)?+)

但它剥夺了时间或书名/年等

This is a sample text/string with a mix of numbers and phone numbers:

oh my god i have 1000 things to do today and i want to finish reading 20,000 Leagues Under the Sea, 1954 book, but hey, I will see you there from 4pm to 8 my cell is 374-7657654 or you can call me at (374)-334-5674 and then i can come from 16:30 thru 19:45 (in european time!)

how do i remove/detect ONLY phone numbers (written in any format, since i can't foresee how a user would write them?

i tried

(\(?+[0-9]{3,15}+[\- ]?+[0-9]?+\)?+)

but it strips away the time or book title/ year etc

最满意答案

这是正则表达式:

1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))?

preg_match("1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))?", "", $text);

$text是您要验证的文本。

所有归功于个人,回答这个问题

Here's the regex:

1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))?

preg_match("1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))?", "", $text);

Where $text is the text you wish to validate.

All credit to indiv, answering this question

更多推荐