[BNM] regex help

Ali ali at nubz.com
Fri Aug 10 11:05:20 BST 2007


Hi

I have snaffled some regex to use in converting plain text links to hyperlinks:

function replaceLinks($text) {

    // convert name at domain.tld into
    // <a href="mailto:name at domain.tld">
    // name at domain.tld</a>

    $text = ereg_replace('[-a-z0-9!#$%&\'*+/=?^_`{|}~]+@([.]?[a-zA-Z0-9_/-])*',
        '<a href="mailto:\\0">\\0</a>',$text);

    // convert http://www.domain.tld/path/file/ into
    // <a href="http://www.domain.tld/path/file/">
    // www.domain.tld/path/file/</a>

    $text = ereg_replace('[a-zA-Z]+://(([.]?[a-zA-Z0-9_/-])*)',
        '<a href="\\0">\\1</a>',$text);

    // convert www.domain.tld/path/file/ into
    // <a href="http://www.domain.tld/path/file/">
    // www.domain.tld/path/file/</a>

    $text = ereg_replace('(^| )(www([-]*[.]?[a-zA-Z0-9_/-?&%])*)',
        ' <a href="http://\\2">\\2</a>',$text);
   
    return $text;
}

all works a treat except when the www.domain.tld starts a new line, only works when it's preceded by a whitespace - can anyone see what needs to change to accomodate this - my fiddling only serves to obliterate the whole function...

cheers,

Ali


More information about the BNMlist mailing list. Powered by Wessex Networks