[BNM] regex help

Ali ali at nubz.com
Fri Aug 10 14:57:35 BST 2007


thanks for the bold try James!

still fails to convert any www. links that follow a carriage return - still works fine within a sentence (i.e. with space in front or if I put a space at the beginning of new line) - I am sure it's sumit to do with the regex condition for the www bit not finding \nwww and that it's not actually going through the filter in the first place in order to have your space added....

still, could get me learning the stringmasters language....

thanks again

Ali

----- 
  From: James Moss 
  To: Brighton New Media 
  Sent: Friday, August 10, 2007 11:19 AM
  Subject: Re: [BNM] regex help



  The similar function I normally use for this simply adds a space to the
  front of the string before processing it then removes it before
  returning it.

  Ive updated yours for you:

  function replaceLinks($text) {

      $text = ' '.$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);
     
      $text = ltrim($text);

      return $text;
  }


  -----Original Message-----
  From: bnmlist-bounces at brightonnewmedia.org
  [mailto:bnmlist-bounces at brightonnewmedia.org] On Behalf Of Ali
  Sent: 10 August 2007 11:05 AM
  To: Brighton New Media
  Subject: [BNM] regex help

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

  BNM Subscribe/Unsubscribe:
  http://www.brightonnewmedia.org/options/bnmlist

  BNM info/archives:
  http://www.brightonnewmedia.org/

  BNM powered by Wessex Networks:
  http://www.wessexnetworks.com
  -- 

  BNM Subscribe/Unsubscribe:
  http://www.brightonnewmedia.org/options/bnmlist

  BNM info/archives:
  http://www.brightonnewmedia.org/

  BNM powered by Wessex Networks:
  http://www.wessexnetworks.com


  -- 
  No virus found in this incoming message.
  Checked by AVG Free Edition. 
  Version: 7.5.476 / Virus Database: 269.11.11/944 - Release Date: 09/08/2007 14:44



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