简体   繁体   中英

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:

$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) { }

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

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.

and you go Rock . and also check the string you don't have +44 there.

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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