开发者

Regular expression: replace email address, with a twist

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 john@doe.com. 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 john@doe.com(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 wh开发者_如何学Pythonat'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;
}


Easy option might be checking with strpos for presence of parenthesis just after e-mail and then using regex to find first occurrence of ((.+?)) after the e-mail.

The other option is to add ((.+?))? to your regex, the last question mark will make the group optional.

Then the fool code will look like:

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++) {
            if(count($the_addrs[$i]) == 4)
                $repaddr[$a] = preg_replace($addr_pattern, '$1(replace this parenthesis by @)$2.$3', $the_addrs[$a]);
            else
                $repaddr[$a] = preg_replace($addr_pattern, '$1(replace this parenthesis by @)$2.$3', $the_addrs[$a]);
        }
        $cc = str_replace($the_addrs, $repaddr, $content);
        return $cc;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜