Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Use <a href="http://www.regular-expressions.info/lookaround.html" rel="nofollow">lookarounds</a></strong></p> <p>Here we're making sure there is a preceding <code>(</code> character, then we look for text we would see in a date formatted like your example. This little bit of code says ALLOW for alpha numeric characters, a literal space character, and a comma, as well as digits <code>([A-Za-z ,\d]+)?</code>. The <code>+</code> character means <em>at least 1</em>. It's not as greedy as <code>.*</code> or <code>.+</code>. I'm surrounding it with parenthesis and then adding a <code>?</code> character to make it not required. It works similar to your <code>|</code> <em>or</em> statement logically because it will still find the year, but we're not making PHP do more work by parsing another check. Then we find the year (always 4 digits <code>{4}</code>). Then we check to make sure it's followed by a literal <code>)</code> character. The look behind <code>(?&lt;=\()</code> and the look ahead <code>(?=\))</code> will find a match, but they are not included in the match results, leaving your answer clean.</p> <p>Since <a href="http://us1.php.net/manual/en/function.preg-match.php" rel="nofollow"><code>preg_match()</code></a> returns an <code>array()</code> we're catching the first element in the array. If you're looking for multiple matches in the same string you can use <code>preg_match_all</code>.</p> <pre><code>$data = '&lt;a href="not important"&gt; &lt;span class="theclass"&gt;data (not important)&lt;/span&gt;&lt;/a&gt; &lt;span class="anotherclass"&gt;extra data (October 1, 2010)&lt;/span&gt; &lt;span class="anotherclass"&gt;extra data (2011)&lt;/span&gt;'; $pattern = '!(?&lt;=\()([A-Za-z ,\d]+)?[\d]{4}(?=\))!'; $res = preg_match_all($pattern,$data,$myDate); print_r($myDate[0]); </code></pre> <p><strong>output</strong></p> <pre><code>Array ( [0] =&gt; October 1, 2010 [1] =&gt; 2011 ) </code></pre> <p>If you're only looking for one match you would change the code to this:</p> <pre><code>$res = preg_match($pattern,$data,$myDate); echo($myDate[0]); </code></pre> <p><strong>Output</strong></p> <pre><code>October 1, 2010 </code></pre> <p>Another way to write the pattern would be like this... we've removed the parenthesis (grouping) and the plus <code>+</code> modifier followed by the conditional <code>?</code>, but left the first set. Then we're using a <code>*</code> to make it conditional. The difference is with preg_match and preg_match_all, any groupings are also stored in the array. Since this isn't a group, then it will not store extra array elements.</p> <pre><code>$pattern = '!(?&lt;=\()[A-Za-z ,\d]*[\d]{4}(?=\))!'; </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.
 

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