Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can resolve this using a <a href="http://www.php.net/manual/en/regexp.reference.assertions.php" rel="noreferrer">lookahead assertion</a>. Basically what we're saying is I want a series of specific letters, (e, ex, ext, x, extension) followed by one or more number. But we also want to cover the case where there's no extension at all.</p> <blockquote> <p>Side Note, you don't need brackets around single characters like [\s] or that [x] that follows. Also, you can group characters that are meant to be in the same spot, so instead of \s?\.?/?, you can use [\s\./]? which means "one of any of those characters"</p> </blockquote> <p>Here's an update with regex that resolves your comment here as well. I've added the explanation in the actual code.</p> <pre><code>&lt;?php $sPattern = "/^ (?: # Area Code (?: \( # Open Parentheses (?=\d{3}\)) # Lookahead. Only if we have 3 digits and a closing parentheses )? (\d{3}) # 3 Digit area code (?: (?&lt;=\(\d{3}) # Closing Parentheses. Lookbehind. \) # Only if we have an open parentheses and 3 digits )? [\s.\/-]? # Optional Space Delimeter )? (\d{3}) # 3 Digits [\s\.\/-]? # Optional Space Delimeter (\d{4})\s? # 4 Digits and an Optional following Space (?: # Extension (?: # Lets look for some variation of 'extension' (?: (?:e|x|ex|ext)\.? # First, abbreviations, with an optional following period | extension # Now just the whole word ) \s? # Optionsal Following Space ) (?=\d+) # This is the Lookahead. Only accept that previous section IF it's followed by some digits. (\d+) # Now grab the actual digits (the lookahead doesn't grab them) )? # The Extension is Optional $/x"; // /x modifier allows the expanded and commented regex $aNumbers = array( '123-456-7890x123', '123.456.7890x123', '123 456 7890 x123', '(123) 456-7890 x123', '123.456.7890x.123', '123.456.7890 ext. 123', '123.456.7890 extension 123456', '123 456 7890', '123-456-7890ex123', '123.456.7890 ex123', '123 456 7890 ext123', '456-7890', '456 7890', '456 7890 x123', '1234567890', '() 456 7890' ); foreach($aNumbers as $sNumber) { if (preg_match($sPattern, $sNumber, $aMatches)) { echo 'Matched ' . $sNumber . "\n"; print_r($aMatches); } else { echo 'Failed ' . $sNumber . "\n"; } } ?&gt; </code></pre> <p>And The Output:</p> <pre><code>Matched 123-456-7890x123 Array ( [0] =&gt; 123-456-7890x123 [1] =&gt; 123 [2] =&gt; 456 [3] =&gt; 7890 [4] =&gt; 123 ) Matched 123.456.7890x123 Array ( [0] =&gt; 123.456.7890x123 [1] =&gt; 123 [2] =&gt; 456 [3] =&gt; 7890 [4] =&gt; 123 ) Matched 123 456 7890 x123 Array ( [0] =&gt; 123 456 7890 x123 [1] =&gt; 123 [2] =&gt; 456 [3] =&gt; 7890 [4] =&gt; 123 ) Matched (123) 456-7890 x123 Array ( [0] =&gt; (123) 456-7890 x123 [1] =&gt; 123 [2] =&gt; 456 [3] =&gt; 7890 [4] =&gt; 123 ) Matched 123.456.7890x.123 Array ( [0] =&gt; 123.456.7890x.123 [1] =&gt; 123 [2] =&gt; 456 [3] =&gt; 7890 [4] =&gt; 123 ) Matched 123.456.7890 ext. 123 Array ( [0] =&gt; 123.456.7890 ext. 123 [1] =&gt; 123 [2] =&gt; 456 [3] =&gt; 7890 [4] =&gt; 123 ) Matched 123.456.7890 extension 123456 Array ( [0] =&gt; 123.456.7890 extension 123456 [1] =&gt; 123 [2] =&gt; 456 [3] =&gt; 7890 [4] =&gt; 123456 ) Matched 123 456 7890 Array ( [0] =&gt; 123 456 7890 [1] =&gt; 123 [2] =&gt; 456 [3] =&gt; 7890 ) Matched 123-456-7890ex123 Array ( [0] =&gt; 123-456-7890ex123 [1] =&gt; 123 [2] =&gt; 456 [3] =&gt; 7890 [4] =&gt; 123 ) Matched 123.456.7890 ex123 Array ( [0] =&gt; 123.456.7890 ex123 [1] =&gt; 123 [2] =&gt; 456 [3] =&gt; 7890 [4] =&gt; 123 ) Matched 123 456 7890 ext123 Array ( [0] =&gt; 123 456 7890 ext123 [1] =&gt; 123 [2] =&gt; 456 [3] =&gt; 7890 [4] =&gt; 123 ) Matched 456-7890 Array ( [0] =&gt; 456-7890 [1] =&gt; [2] =&gt; 456 [3] =&gt; 7890 ) Matched 456 7890 Array ( [0] =&gt; 456 7890 [1] =&gt; [2] =&gt; 456 [3] =&gt; 7890 ) Matched 456 7890 x123 Array ( [0] =&gt; 456 7890 x123 [1] =&gt; [2] =&gt; 456 [3] =&gt; 7890 [4] =&gt; 123 ) Matched 1234567890 Array ( [0] =&gt; 1234567890 [1] =&gt; 123 [2] =&gt; 456 [3] =&gt; 7890 ) Failed () 456 7890 </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. 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