Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's break the code into two lines.</p> <pre><code>preg_replace("~[^0-9]~", "", $phone); </code></pre> <p>First, we're going to replace matches to a regex with an empty string (in other words, delete matches from the string). The regex is <code>[^0-9]</code> (the <code>~</code> on each end is a <a href="http://ca.php.net/manual/en/regexp.reference.delimiters.php" rel="nofollow noreferrer">delimiter</a>). <code>[...]</code> in a regex defines a <a href="http://www.regular-expressions.info/charclass.html" rel="nofollow noreferrer">character class</a>, which tells the regex engine to match one character within the class. Dashes are generally special characters inside a character class, and are used to specify a <em>range</em> (ie. <code>0-9</code> means all characters between <code>0</code> and <code>9</code>, inclusive).</p> <p>You can think of a character class like a shorthand for a big <code>OR</code> condition: ie. <code>[0-9]</code> is a shorthand for <code>1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9</code>. Note that classes don't have to contain ranges, either -- <code>[aeiou]</code> is a character class that matches <code>a or e or i or o or u</code> (or in other words, any vowel).</p> <p>When the first character in the class is <code>^</code>, the class is <em>negated</em>, which means that the regex engine should match any character that <em>isn't</em> in the class. So when you put all that together, the first line removes anything that isn't a digit (a character between <code>0</code> and <code>9</code>) from <code>$phone</code>.</p> <pre><code>preg_match('~([0-9]{3})([0-9]{3})([0-9]{4})~', $phone, $matches); </code></pre> <p>The second line tries to match <code>$phone</code> against a second expression, and puts the results into an array called <code>$matches</code>, if a match is made. You will note there are three sets of <a href="http://www.regular-expressions.info/brackets.html" rel="nofollow noreferrer">brackets</a>; these define <em>capturing groups</em> -- ie. if there is a match of a pattern as a whole, you will end up with three <em>submatches</em>, which in this case will contain the area code, prefix and suffix of the phone number. In general, anything contained in brackets in a regular expression is capturing (while there are exceptions, they are beyond the scope of this explanation). Groups can be useful for other things too, without wanting the overhead of capturing, so a group can be made <em>non-capturing</em> by prefacing it with <code>?:</code> (ie. <code>(?:...)</code>).</p> <p>Each group does a similar thing: <code>[0-9]{3}</code> or <code>[0-9]{4}</code>. As we saw above, <code>[0-9]</code> defines a character class containing the digits between 0 and 9 (as the classes here don't start with <code>^</code>, these are not negated groups). The <code>{3}</code> or <code>{4}</code> is a <a href="http://www.regular-expressions.info/repeat.html" rel="nofollow noreferrer">repetition</a> operator, which says "match <em>exactly</em> 3 (or 4) of the previous token (or group)". So <code>[0-9]{3}</code> will match exactly three digits in a row, and <code>[0-9]{4}</code> will match exactly four digits in a row. Note that the digits don't have to be all the same (ie. <code>111</code>), because the character class is evaluate for each repetition (so <code>123</code> will match because 1 matches <code>[0-9]</code>, then 2 matches <code>[0-9]</code>, and then 3 matches <code>[0-9]</code>).</p>
    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