dlofrodloh
dlofrodloh

Reputation: 1744

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?

Upvotes: 0

Views: 85

Answers (1)

Ali Shan
Ali Shan

Reputation: 636

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

Upvotes: 1

Related Questions