April 20, 2012

Prevent links from auto hyperlinking in Outlook etc using PHP

Question by Ben Carey

I know that this can be done using settings in Outlook, but that only sorts the issue for myself.

What I would like to do is use PHP to prevent text from being hyperlinked just because there is an @ sign etc…

As far as I can see, the only option for me is to encode all @ signs to their HTML numeric entity like so:

Something like this:

$message = str_replace('@','@',$message);

However, if possible, I do not want this to happen if the @ sign is part of an email address.

Therefore I need something like this:

// SOME_REGEX will match any @ sign that is NOT part of an email address
$message = preg_replace('SOME_REGEX','@',$message);

Can anybody think of any other better methods? Are there any flaws in this plan? Can anyone suggest a good regular expression for this? I am struggling to write a regex that matches an @ sign if it is not part of an email address

Thanks in advance

Answer by Anthony

This will not work if the email address is wrapped in anything not defined in the trim list.

$chunked_message = explode(" ", $message);

foreach($chunked_message as $chunk) {
    $clean_chunked_message[] = 
               (!filter_var(trim($chunk, " -().?!trn", FILTER_VALIDATE_EMAIL)) 
               ? str_replace('@', '@' $chunk) : $chunk;
}

$clean_message = implode(" ", $clean_chunked_message);

Good luck.

Answer by Starx

This is a feature of mail application to detect link when it is found and make it click able.

A dirty trick to escape this situation is to use space in between the links.

Example:

http://ww w.you tube.com/v=.......

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!