PHP正则表达式从+到第一个非数字中提取电话号码


PHP Regex to extract phone number from + to first non-number

编辑:我这样做是因为提供给我的数据以这种格式有数百个换行分隔的条目,我需要将微格式合并到地址数据中。因此,如果提供的字符串如下所示,我需要输出:

<p><span itemprop="telephone">+1 800 123 456</span> (toll free) from overseas</p>

,

我需要一个正则表达式来从下面的格式中提取一个电话号码:

+1 800 123 456(海外免费)

我所提供的数据一直以这种格式输入,因此有效地,一个正则表达式可以获得从"+"到第一个非数字字符的所有内容。

如果你想使用正则表达式,你可以这样做:

'+['d's]+
$re = '/('+['d's]+)/'; 
$str = "+1 800 123 456 (toll free) from overseas'n"; 
preg_match_all($re, $str, $matches);

另一方面,您可以使用castis建议的方法,使用preg_replace将不需要的字符替换为空字符串,并保留其余部分,例如:

preg_replace('/['D'+]/', '', $phone_number);