简体   繁体   English

识别并找到英国手机号码在字符串中的位置

[英]Identifying and finding the position of a UK mobile number in a string

I want to be able to read a text file and identify mobile numbers and their positions in the string. 我希望能够读取文本文件并识别手机号码及其在字符串中的位置。 It must be able to identify numbers even when there's spaces between the numbers. 即使数字之间有空格,它也必须能够识别数字。 I've tried the following, but the result is false when it should be an array containing the two mobile numbers: 我尝试了以下操作,但是当它应该是包含两个手机号码的数组时,结果为false:

$fstring="A UK mobile number: 07765789098... here's another: 077 65362 723";
$pattern = "^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$^";
preg_match ($pattern, $fstring, $matches, PREG_OFFSET_CAPTURE);

var_dump($matches);

Gives me: 给我:

array(0) { } 数组(0){}

Is the problem with the regex? 正则表达式有问题吗?

Try This regex 试试这个正则表达式

$pattern = "/(\\S44+[7]+\\d{9})|(\\S44+\\s+[07]+\\d{1}+\\s+\\d{5}+\\s+\\d{3})/";

Will match +4477657890983 +44 077 65362 723 将匹配+4477657890983 +44 077 65362 723

First part will not look for the start of line or string it'll find the phone no anywhere and after that i'll find if it have 7 or 07 as different type styles and after that it'll match 9 digits. 第一部分不会寻找行首或字符串的开头,它将在任何地方都找不到电话,之后我会发现它是否具有7或07作为不同的字体样式,然后它将匹配9位数字。

and you go Rock . 然后你去摇滚。 and also check the string you don't have +44 there. 并检查您那里没有+44的字符串。

$pattern = "/((\\S44)?+0?[7]+\\d{9})|((\\S44+\\s)?+[07]+\\d{1}+\\s+\\d{5}+\\s+\\d{3})/";

This will match 077657890981 077 65362 7232 +4477657890983 +44 077 65362 723 这将与077657890981 077 65362 7232 +4477657890983 +44 077 65362 723

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM