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).
So for instance, the function receives:
$content = "Hello [email protected]. How are you today?";
and outputs:
$content = "Hello <span class="email">john(replace this parenthesis by @)example.com</span>. How are you today?";
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:
$content = "Hello [email protected](John Doe). How are you today?";
the new output would be:
$content = "Hello <span class="email" title="John Doe">john(replace this parenthesis by @)example.com</span>. How are you today?";
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.
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).
Any pointer would be helpful, here is my current 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 < count($the_addrs); $a++) {
$repaddr[$a] = preg_replace($addr_pattern, '<span class="email" title="$4">$1(replace this parenthesis by @)$2.$3</span>', $the_addrs[$a]);
}
$cc = str_replace($the_addrs, $repaddr, $content);
return $cc;
}