使用 PHP 解析字符串中的电话号码

发布于 2024-10-10 08:17:34 字数 2071 浏览 6 评论 0原文

我需要解析字符串中的电话号码,每个字符串可能有多个数字。我的问题是电话号码可能会出现这样的情况:

912343267
91 234 32 67
912 343 267
34912343267
+34912343267
0034912343267
+34 912343267
+34 91 234 32 67
+34 912 343 267

我该如何处理这个问题?如果您有任何线索,将不胜感激。

最好的问候,


Updade1:

我正在像在真实环境中一样测试代码:

$phone_list = "912343267 91 fgf ddf 234 32 67 dfffgg g 912 343 267 ffd dff fff 34912343267 ddssf f +34912343267 f fdd d 0034912343267 derd df e +34 912343267 fdd ff +34 91 234 32 67 ffd vv ff f +34 912 343 267";
$string = preg_replace('~[^0-9]~','',$phone_list);
echo $string;

给我:

91234326791234326791234326734912343267349123432670034912343267349123432673491234326734912343267

可以输出数组中的数字吗?

最好的问候,


Update2:

我已经用另一种字符串进行了测试,但失败了。如果有人对此有任何线索,我将发布该示例。

$phone_list = '</div>A Front para<br /><br /><br /><br /><br /><br />-Apoio;<br />-Criação;<br />-Campanhas;<br />-Promoções<br /><br /><br />CONDIÇÕES:<br /><br />Local de Trabalho: Es<br />Folgas: Mistas<br /><br /><br /><br />ordem 500€<br /><br /><br /><br />Mínimos:<br /><br />- Conhecimentos;<br />- Ensino ;<br />-INGLÊS.<br /><br /><br /><br />Candidaturas: <br />[email protected]<br />218559372 | 927 555 929 | <br />RH<br />Rua C. Sal. 40<br />1000-000 Lisboa<br /><br /><br />';
$phone_list = preg_replace('~[^0-9a-z]~i','',$phone_list);
$phone_list = preg_split('~[a-z]+~i',$phone_list);
print_r($phone_list);

代码返回:

Array ( [0] => [1] => 500 [2] => 218559372927555929 [3] => 40 [4] => 1000000 [5] => ) 

代码应解析:218559372 和 927555929 作为单独的数字。

有这方面的线索吗?

此致,

I need to parse phone numbers in strings, each string could have more than one number. My problem is that phone number could appear like this:

912343267
91 234 32 67
912 343 267
34912343267
+34912343267
0034912343267
+34 912343267
+34 91 234 32 67
+34 912 343 267

How can I possible deal with this? If you have some clue, would be appreciated.

Best Regards,


Updade1:

I'm testing the code like in the real environment:

$phone_list = "912343267 91 fgf ddf 234 32 67 dfffgg g 912 343 267 ffd dff fff 34912343267 ddssf f +34912343267 f fdd d 0034912343267 derd df e +34 912343267 fdd ff +34 91 234 32 67 ffd vv ff f +34 912 343 267";
$string = preg_replace('~[^0-9]~','',$phone_list);
echo $string;

Gives me:

91234326791234326791234326734912343267349123432670034912343267349123432673491234326734912343267

It is possible to output the numbers in an array?

Best Regards,


Update2:

I have tested with another kind of string but fails. I will post the example if someone have any clues on this.

$phone_list = '</div>A Front para<br /><br /><br /><br /><br /><br />-Apoio;<br />-Criação;<br />-Campanhas;<br />-Promoções<br /><br /><br />CONDIÇÕES:<br /><br />Local de Trabalho: Es<br />Folgas: Mistas<br /><br /><br /><br />ordem 500€<br /><br /><br /><br />Mínimos:<br /><br />- Conhecimentos;<br />- Ensino ;<br />-INGLÊS.<br /><br /><br /><br />Candidaturas: <br />[email protected]<br />218559372 | 927 555 929 | <br />RH<br />Rua C. Sal. 40<br />1000-000 Lisboa<br /><br /><br />';
$phone_list = preg_replace('~[^0-9a-z]~i','',$phone_list);
$phone_list = preg_split('~[a-z]+~i',$phone_list);
print_r($phone_list);

The code return:

Array ( [0] => [1] => 500 [2] => 218559372927555929 [3] => 40 [4] => 1000000 [5] => ) 

The code should parse: 218559372 and 927555929 as separate numbers.

Any clues on this?

Best Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

成熟稳重的好男人 2024-10-17 08:17:34
$string = preg_replace('~[^0-9]~','',$string);
$string = preg_replace('~[^0-9]~','',$string);
七月上 2024-10-17 08:17:34

根据您更新中的示例字符串,这可能是您最可靠的解决方案...但它有一个主要警告,即只有当字符串中的所有数字都是电话号码的一部分时(没有其他随机数字),它才会起作用。数字不是电话号码的一部分)...

$phone_list = "912343267 91 fgf ddf 234 32 67 dfffgg g 912 343 267 ffd dff fff 34912343267 ddssf f +34912343267 f fdd d 0034912343267 derd df e +34 912343267 fdd ff +34 91 234 32 67 ffd vv ff f +34 912 343 267";
$phone_list = preg_replace('~[^0-9a-z]~i','',$phone_list);
$phone_list = preg_split('~[a-z]+~i',$phone_list);

如果您的字符串中还有其他不是电话号码的数字...那么您就有点SoL...正如我之前提到的,您需要后退一步并重做您最初获取/存储字符串的方式。

Based on your example string in your update, this is probably going to be your most reliable solution...but it has the major caveat that it will only work if ALL numbers in your string will be part of a phone number (no other random numbers not part of a phone number)...

$phone_list = "912343267 91 fgf ddf 234 32 67 dfffgg g 912 343 267 ffd dff fff 34912343267 ddssf f +34912343267 f fdd d 0034912343267 derd df e +34 912343267 fdd ff +34 91 234 32 67 ffd vv ff f +34 912 343 267";
$phone_list = preg_replace('~[^0-9a-z]~i','',$phone_list);
$phone_list = preg_split('~[a-z]+~i',$phone_list);

If there will be other numbers in your string that are not phone numbers...then you're kinda SoL...as I mentioned before, you will need to take a step back and redo how you are getting/storing the string in the first place.

倾城泪 2024-10-17 08:17:34

如果您只想从字符串中获取数字,请使用 preg_split() 创建一个仅包含数字的数组,然后使用以下函数,该函数具有一个用于国家/地区代码的可选参数:

function formatPhone($phone, $country_code = 34) {
    $pattern = '/^(00|\+)?' . $country_code . '\s?/';
    $phone = preg_replace($pattern, '', $phone);
    return str_replace(' ', '', $phone);
}
$list = preg_split('/\s([^\d+]+)/', $phone_list);
$numbers = array();
foreach ($list as $number) {
    $numbers[] = formatPhone($number);
}

$numbers 是格式化数字的数组。

以下内容应处理您的更新 2 代码:

$country_code = 34;
$pattern = '/(?:\+|00)?(?:' . $country-code . ')?(\d[\d\s]{9,14})/';
preg_match_all($pattern, $phone_list, $matches);
$numbers = array();
if (isset($matches[1])) {
    foreach ($matches[1] as $match) {
        $numbers[] = str_replace(' ', '', $match);
    }
}

您格式化的电话号码位于 $numbers 中。

If it's just the numbers that you want from the string, use preg_split() to create an array containing only the numbers, then use the following function, which has an optional parameter for the country code:

function formatPhone($phone, $country_code = 34) {
    $pattern = '/^(00|\+)?' . $country_code . '\s?/';
    $phone = preg_replace($pattern, '', $phone);
    return str_replace(' ', '', $phone);
}
$list = preg_split('/\s([^\d+]+)/', $phone_list);
$numbers = array();
foreach ($list as $number) {
    $numbers[] = formatPhone($number);
}

$numbers is an array of the formatted numbers.

The following should handle your Update 2 code:

$country_code = 34;
$pattern = '/(?:\+|00)?(?:' . $country-code . ')?(\d[\d\s]{9,14})/';
preg_match_all($pattern, $phone_list, $matches);
$numbers = array();
if (isset($matches[1])) {
    foreach ($matches[1] as $match) {
        $numbers[] = str_replace(' ', '', $match);
    }
}

Your formatted phone numbers are in $numbers.

你在我安 2024-10-17 08:17:34

那么,您新更新的示例内容这次是真正的真实内容吗?为了清楚起见,进行了细分:

$phone_list = '
</div>
A Front para
<br /><br /><br /><br /><br /><br />
-Apoio;
<br />
-Criação;
<br />
-Campanhas;
<br />
-Promoções
<br /><br /><br />
CONDIÇÕES:
<br /><br />
Local de Trabalho: Es
<br />
Folgas: Mistas
<br /><br /><br /><br />
ordem 500€
<br /><br /><br /><br />
Mínimos:
<br /><br />
- Conhecimentos;
<br />
- Ensino ;
<br />
-INGLÊS.
<br /><br /><br /><br />
Candidaturas: 
<br />
[email protected]
<br />
218559372 | 927 555 929 | 
<br />
RH
<br />
Rua C. Sal. 40
<br />
1000-000 Lisboa
<br /><br /><br />';

为了从中提取数字,您必须有一种可靠的方法来查找标记数字所在位置的某种锚点或分隔符。例如,如果整个字符串和 html 代码本身不会改变,您可以查找倒数第 7 个和第 6 个
标记之间的内容,然后转到从那里...

...从那里,看起来如果您有多个电话号码,它们是由管道分隔的,这是正确的吗?你为什么一开始就不说这件事呢?

我写这篇文章只是为了尝试向您解释正则表达式的工作原理:为了可靠地匹配模式,必须首先识别格式中的可靠模式。

So is your new updated example content the really real for real this time real content? Broken down for clarity:

$phone_list = '
</div>
A Front para
<br /><br /><br /><br /><br /><br />
-Apoio;
<br />
-Criação;
<br />
-Campanhas;
<br />
-Promoções
<br /><br /><br />
CONDIÇÕES:
<br /><br />
Local de Trabalho: Es
<br />
Folgas: Mistas
<br /><br /><br /><br />
ordem 500€
<br /><br /><br /><br />
Mínimos:
<br /><br />
- Conhecimentos;
<br />
- Ensino ;
<br />
-INGLÊS.
<br /><br /><br /><br />
Candidaturas: 
<br />
[email protected]
<br />
218559372 | 927 555 929 | 
<br />
RH
<br />
Rua C. Sal. 40
<br />
1000-000 Lisboa
<br /><br /><br />';

In order to scrape the numbers out of that, you must have a reliable way of finding anchors or delimiters of some kind that mark where the numbers are. For example, if this the entirety of the string and the html code itself will not change, you can for instance look for the content between the 7th and 6th to last <br /> tags and go from there....

...from there, it looks like if you have multiple phone numbers, they are delimited by a pipe, is this correct? Why didn't you say anything about that to begin with?

I'm only making this post to try and explain to you how regex works: in order to reliably match a pattern, a reliable pattern in the format must first be identified.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文