简体   繁体   中英

Find a valid UK telephone number in a string

I have an input form where I want to strip out a telephone number if entered. I've found an expression that finds the variety of UK numbers, but it only works if the only thing that is entered is a number. I need the regex to find any instance of a number within the text.

Here is what I have so far

if (preg_match_all('/^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$/', $post, $match_num))
{
    $error[0] = 1;
    $error[2] = 'error text';
    $error[4] = $match_num;
}

Just remove the ^ and $ from the start and end. This will allow the regex to match anywhere.

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