简体   繁体   English

在字符串中查找有效的英国电话号码

[英]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. 这将使正则表达式可以在任何地方匹配。

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

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