title: Regular expression: replace email address, with a twist
body: <p>I have developed a wordpress plugin that looks through a bunch of html and detects any email address, replacing them by a non harvestable html markup (to be rerendered as an email via javascript for better usability).</p>
  
  <p>So for instance, the function receives:</p>
  
  <pre><code>$content = "Hello john@doe.com. How are you today?";
  </code></pre>
  
  <p>and outputs:</p>
  
  <pre><code>$content = "Hello &lt;span class="email"&gt;john(replace this parenthesis by @)example.com&lt;/span&gt;. How are you today?";
  </code></pre>
  
  <p>My function works fine, but i would like now, to give an option to specify what the readable email should be like. So if the function receives:</p>
  
  <pre><code>$content = "Hello john@doe.com(John Doe). How are you today?";
  </code></pre>
  
  <p>the new output would be:</p>
  
  <pre><code>$content = "Hello &lt;span class="email" title="John Doe"&gt;john(replace this parenthesis by @)example.com&lt;/span&gt;. How are you today?";
  </code></pre>
  
  <p>So the regex should look for attached parenthesis, and if found, take what's inside and add a html title attribute, remove the parenthesis, and then parse the email.</p>
  
  <p>I'm pretty much clueless as to how to make it happen, because of the optional nature of the feature (meaning: those parenthesis won't always be there).</p>
  
  <p>Any pointer would be helpful, here is my current code:</p>
  
  <pre><code>function pep_replace_excerpt($content) {
       $addr_pattern = '/([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})/i';
          preg_match_all($addr_pattern, $content, $addresses);
          $the_addrs = $addresses[0];
          for ($a = 0; $a &lt; count($the_addrs); $a++) {
              $repaddr[$a] = preg_replace($addr_pattern, '&lt;span class="email" title="$4"&gt;$1(replace this parenthesis by @)$2.$3&lt;/span&gt;', $the_addrs[$a]);
          }
          $cc = str_replace($the_addrs, $repaddr, $content);
          return $cc;
  }
  </code></pre>