title: Detecting emails in a text
body: <p>I'm trying to create a function that translates every occurrence of a plain text email address in a given string into it's <em>htmlized</em> version.</p>
  <p>Let's say I have the following code, where <code>htmlizeEmails</code> is the function I'm looking for:</p>
  <pre><code>$str = &quot;Send me an email to bob@example.com.&quot;;
  echo htmlizeEmails($str); // Echoes &quot;Send me an email to &lt;a href=&quot;mailto:bob@example.com&quot;&gt;bob@example.com&lt;/a&gt;.&quot;
  </code></pre>
  <p>If possible, I'd like this function to use the <a href="http://www.php.net/manual/en/function.filter-var.php" rel="noreferrer"><code>filter_var</code></a> function to check if the email is valid.</p>
  <p>Does anyone know how to do this? Thanks!</p>
  <h3>Edit:</h3>
  <p>Thanks for the answers, I used Shocker's regex to match potential email addresses and then, <strong>only</strong> if the <code>filter_var</code> validates it, it gets replaced.</p>
  <pre><code>function htmlizeEmails($text)
      preg_match_all('/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})/', $text, $potentialEmails, PREG_SET_ORDER);
      
      $potentialEmailsCount = count($potentialEmails);
      for ($i = 0; $i &lt; $potentialEmailsCount; $i++) {
          if (filter_var($potentialEmails[$i][0], FILTER_VALIDATE_EMAIL)) {
              $text = str_replace($potentialEmails[$i][0], '&lt;a href=&quot;mailto:' . $potentialEmails[$i][0] .'&quot;&gt;' . $potentialEmails[$i][0] .'&lt;/a&gt;', $text);
          }
      }
  }
  </code></pre>