[BNM] regex help

Ali ali at nubz.com
Fri Aug 10 15:21:17 BST 2007


thanks david

unfortunately:

Warning: preg_replace() [function.preg-replace]: Unknown modifier '(' 


Ali
  ----- Original Message ----- 
  From: David Pashley 
  To: Brighton New Media 
  Sent: Friday, August 10, 2007 3:09 PM
  Subject: Re: [BNM] regex help


  On Aug 10, 2007 at 11:05, Ali praised the llamas by saying:
  > 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);

  Frankly, I'd use the PCRE stuff. It has fun things like negative
  look-behind assertions:

       $text = preg_replace('(?<!://)(www([-]*[.]?[a-zA-Z0-9_/-?&%])*)',
           '<a href="http://$2">$2</a>',$text);

  I'm mostly assuming the rest of the regex is correct.

        
  >    
  >     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