php regex , 从 text/html 中提取电话号码 [英] php regex , extract phone number from text/html

查看:56
本文介绍了php regex , 从 text/html 中提取电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
php正则表达式,从html文档中提取类似电话号码的正则表达式

我正在尝试从不同的 html 页面中提取电话号码.基本上,信息是一个 10 位数字,可能有不同的形式,例如:

<前>000-000-0000000 - 000 - 00000000000000

<前>请注意,000 - 000 - 0000000 不是有效的电话号码,因此如果该号码包含任何其他数字,则不应提取该号码

如果您能帮助我创建适用于所有 3 种情况的完美正则表达式,我将不胜感激.到目前为止,我只能让它对最后一个(最简单的)起作用.

解决方案

如果你想允许 10 位数字的无限组合,那么这样做就行了:

^\D?((?:\d\D*){10})$

Possible Duplicate:
php regex, extract like phone number regex from html documents

I'm trying to extract phone numbers from different html pages. Basically the information is a 10 digits number which may have different forms such :

000-000-0000
000 - 000 - 0000
0000000000

please note that 000 - 000 - 0000000 is not a valid phone number so it should not extract the number if it contains any additional digits

I would appreciate any help to create the perfect regex working on all the 3 situations . So far I could make it work only for the last one (the simplest one ).

解决方案

If you want to allow unlimited combinations of exactly 10 digits, then this will do the trick:

^\D?((?:\d\D*){10})$

这篇关于php regex , 从 text/html 中提取电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆