查找字符串中的电话号码
在 PHP 中,我正在搜索特定文本中的电话号码。 我使用 explode()
将文本分成不同的部分,并使用我正在搜索的城市的区号作为分隔符。问题在于,包含与区号相同号码的电话号码无法正常返回。
例如:
"foofoo 010-1234567 barbar"
分为 "foofoo "
和 "-1234567 barbar"
但
"foofoo 010- 1230107 barbar"
分为 "foofoo "
、"-123"
和 "7野蛮人”
!
我可以使用第一个用区号重建电话号码,但第二个当然会出错......
我想我需要一个正则表达式来使用某种机制来分割文本,以不分割在短字符串上,而不是 explode()
,但我不知道该怎么做。
有什么想法或更好的方法来搜索文本中的电话号码吗?
更新: 格式不一致,因此寻找连字符并不能解决问题。有些电话号码的区号和号码之间有空格,有些有钩子,有些什么都没有,等等。荷兰电话号码的区号为 2,3 或 4 个数字,通常总共有 10 个数字。
In PHP I'm searching for phonenumbers in a certain text.
I use explode()
to divide the text in different parts,using the area code of the city I'm searching for as the delimiter. The problem is that phonenumbers that include the same numbers as the area-code are not returned well.
For example:
"foofoo 010-1234567 barbar"
splits into "foofoo "
and "-1234567 barbar"
but
"foofoo 010-1230107 barbar"
splits into "foofoo "
, "-123"
and "7 barbar"
!
I can use the first one to reconstruct the phonenummer with the areacode, but the second goes wrong of course...
I guess I need a regular expression to split the text with some kind of mechanism to not split on short strings, instead of explode()
, but I don't know how to do it.
Any ideas or a better way to search for phonenumbers in a text ?
UPDATE:
The format is NOT consistent, so looking for the hyphen is no solution. Some phone numbers have spaces between the area code and number, some have hooks, some have nothing, etc. Dutch phonenumbers have an areacode of 2,3 or 4 numbers and are usually 10 numbers in total.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要查找类似以下的电话号码:
请尝试以下操作:
To find phone numbers like:
Try this:
您可以使用正则表达式来匹配电话号码。有很多很多方法可以给这只特定的猫剥皮(这里可能有很多相同的问题),一个超级基本的例子可能如下所示。
上面将输出 $numbers 数组的内容。
You could use a regular expression to match the phone numbers. There are many, many ways to skin this particular cat (and likely many identical questions here on SO) a super-basic example might look like the following.
The above would output the contents of the
$numbers
array.如果删除所有非数字字符,则只剩下电话号码。然后,如果您愿意,您可以将该字符串解析为
###-###-####
。不确定这是否是您正在寻找的。
If you delete all of the non numeric characters, you will only be left with the phone number. You can then take that string and parse it into
###-###-####
if you wish.Not sure if that is what you are looking for or not.