Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The current REGEX</p> <pre><code>/^[\(]?(\d{0,3})[\)]?[\.]?[\/]?[\s]?[\-]?(\d{3})[\s]?[\.]?[\/]?[\-]?(\d{4})[\s]?[x]?(\d*)$/ </code></pre> <p>has a lot of issues, resulting in it matching all of the following, among others:<br> <code>(0./ -000 ./-0000 x00000000000000000000000)</code><br> <code>()./1234567890123456789012345678901234567890</code><br> <code>\)\-555/1212 x</code></p> <p>I think this REGEX is closer to what you're looking for:</p> <pre><code>/^(?:(?:(?:1[.\/\s-]?)(?!\())?(?:\((?=\d{3}\)))?((?(?!(37|96))[2-9][0-8][0-9](?&lt;!(11)))?[2-9])(?:\((?&lt;=\(\d{3}))?)?[.\/\s-]?([0-9]{2}(?&lt;!(11)))[.\/\s-]?([0-9]{4}(?&lt;!(555(01([0-9][0-9])|1212))))(?:[\s]*(?:(?:x|ext|extn|ex)[.:]*|extension[:]?)?[\s]*(\d+))?$/ </code></pre> <p>or, exploded:</p> <pre><code>&lt;? $pattern = '/^ # Matches from beginning of string (?: # Country / Area Code Wrapper [not captured] (?: # Country Code Wrapper [not captured] (?: # Country Code Inner Wrapper [not captured] 1 # 1 - CC for United States and Canada [.\/\s-]? # Character Class ('.', '/', '-' or whitespace) for allowed (optional, single) delimiter between Country Code and Area Code ) # End of Country Code (?!\() # Lookahead, only allowed if not followed by an open parenthesis )? # Country Code Optional (?: # Opening Parenthesis Wrapper [not captured] \( # Opening parenthesis (?=\d{3}\)) # Lookahead, only allowed if followed by 3 digits and closing parenthesis [lookahead never captured] )? # Parentheses Optional ((?(?!(37|96))[2-9][0-8][0-9](?&lt;!(11)))?[2-9]) # 3-digit NANPA-valid Area Code [captured] (?: # Closing Parenthesis Wrapper [not captured] \( # Closing parenthesis (?&lt;=\(\d{3}) # Lookbehind, only allowed if preceded by 3 digits and opening parenthesis [lookbehind never captured] )? # Parentheses Optional )? # Country / Area Code Optional [.\/\s-]? # Character Class ('.', '/', '-' or whitespace) for allowed (optional, single) delimiter between Area Code and Central-office Code ([0-9]{2}(?&lt;!(11))) # 3-digit NANPA-valid Central-office Code [captured] [.\/\s-]? # Character Class ('.', '/', '-' or whitespace) for allowed (optional, single) delimiter between Central-office Code and Subscriber number ([0-9]{4}(?&lt;!(555(01([0-9][0-9])|1212)))) # 4-digit NANPA-valid Subscriber Number [captured] (?: # Extension Wrapper [not captured] [\s]* # Character Class for allowed delimiters (optional, multiple) between phone number and extension (?: # Wrapper for extension description text [not captured] (?:x|ext|extn|ex)[.:]* # Abbreviated extensions with character class for terminator (optional, multiple) [not captured] | # OR extension[:]? # The entire word extension with character class for optional terminator )? # Marker for Extension optional [\s]* # Character Class for allowed delimiters (optional, multiple) between extension description text and actual extension (\d+) # Extension [captured if present], required for extension wrapper to match )? # Entire extension optional $ # Matches to end of string /x'; // /x modifier allows the expanded and commented regex ?&gt; </code></pre> <p>This modification provides several improvements. </p> <ol> <li>It creates a configurable group of items that can match as the extension. You can add additional delimiters for the extension. <strong>This was the original request.</strong> The extension also allows for a colon after the extension delimter.</li> <li>It converts the sequence of 4 optional delimiters (dot, whitespace, slash or hyphen) into a character class that matches only a single one.</li> <li>It groups items appropriately. In the given example, you can have the opening parentheses without an area code between them, and you can have the extension mark (space-x) without an extension. This alternate regular expression requires either a complete area code or none and either a complete extension or none.</li> <li>The 4 components of the number (area code, central office code, phone number and extension) are the back-referenced elements that feed into $matches in <code>preg_match()</code>.</li> <li>Uses lookahead/lookbehind to require matched parentheses in the area code.</li> <li>Allows for a 1- to be used before the number. (This assumes that all numbers are US or Canada numbers, which seems reasonable since the match is ultimately made against NANPA restrictions. Also disallows mixture of country code prefix and area code wrapped in parentheses.</li> <li>It merges in the NANPA rules to eliminate non-assignable telephone numbers. <ol> <li>It eliminates area codes in the form 0xx, 1xx 37x, 96x, x9x and x11 which are invalid NANPA area codes.</li> <li>It eliminates central office codes in the form 0xx and 1xx (invalid NANPA central office codes).</li> <li>It eliminates numbers with the form 555-01xx (non-assignable from NANPA).</li> </ol></li> </ol> <p>It has a few minor limitations. They're probably unimportant, but are being noted here.</p> <ol> <li>There is nothing in place to require that the same delimiter is used repeatedly, allowing for numbers like 800-555.1212, 800/555 1212, 800 555.1212 etc.</li> <li>There is nothing in place to restrict the delimiter after an area code with parentheses, allowing for numbers like (800)-555-1212 or (800)/5551212.</li> </ol> <p>The NANPA rules are adapted from the following REGEX, found here: <a href="http://blogchuck.com/2010/01/php-regex-for-validating-phone-numbers/" rel="nofollow noreferrer">http://blogchuck.com/2010/01/php-regex-for-validating-phone-numbers/</a></p> <pre><code>/^(?:1)?(?(?!(37|96))[2-9][0-8][0-9](?&lt;!(11)))?[2-9][0-9]{2}(?&lt;!(11))[0-9]{4}(?&lt;!(555(01([0-9][0-9])|1212)))$/ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload