Regex - removing a final period from a hyperlink

Discussion in 'PHP' started by chrisman826, Jul 26, 2007.

  1. #1
    Hello!
    I am using some regular expressions that automatically turn URLs into hyperlinkgs (with the <a href= blah blah). The problem that I am having is that i find many times people on my site are adding a period to the end of a URL if used at the end of a sentance (Using correct grammar of course). For example:
    Http://www.somesite.com/index.html.

    so naturally it comes out like this

    <a href="http://www.somesite.com/index.html.">http://www.somesite.com/index.html.</a>

    the dot on the end (most of the time) causes an error 404.

    basically, how can i get rid of the period on the end if its there?

    Im thinking about using another eregi_replace to remove periods from the end of urls, or put a space in between the url and . or something. But i cant figure out how to do that.

    heres my current code:

    
    function makeURL($string){
    
      //make URL's a clickable Hyperlink (beginning with http or ftp ://)
      $string = eregi_replace('([[:space:]()[{}])(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',    '\\1<a href="\\2" target="_blank">\\2</a>', $string);  
      //make URL's a clickable Hyperlink (beginning with www.)
      $string = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',    '\\1<a href="http://\\2" target="blank">\\2</a>', $string);  
    
      //make Email Address a clickable Hyperlink  
      $string = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',    '<a href="mailto:\\1">\\1</a>', $string);  
      
      return $string;
    
    }
    
    Code (php):
    any help is appreciated.
     
    chrisman826, Jul 26, 2007 IP
  2. pruad

    pruad Peon

    Messages:
    71
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    maybe this simple code is fine for you?

    
    
    str_replace(".\">", "\">", $string);
    
    
    Code (markup):
     
    pruad, Jul 26, 2007 IP
  3. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #3
    The first 2 replacements can be combined:
    $string = eregi_replace('([[:space:]()[{}])((((f|ht){1}tp://)|www.)[-a-zA-Z0-9@:%_\+.~#?&//=]+[-a-zA-Z0-9@%_\+~#&//=])',    '\\1<a href="http://\\2" target="blank">\\2</a>', $string);
    PHP:
    That also fixes your problem. pruad's solution would work but this way also prevents problems with other characters, such as a question mark or a closing bracket and doesn't involve another replace.

    Here is the same code with a bit of cleanup:
    $string = eregi_replace('([[:space:]()[{}])((((f|ht)tp://)|www.)[-a-zA-Z0-9@:%_\+.~#?&/=]+[-a-zA-Z0-9@%_\+~#&/=])',    '\\1<a href="http://\\2" target="blank">\\2</a>', $string);
    PHP:
    Also, look around code for other forums or apps that do this to make such regex patterns, it seems you are just reinventing the wheel. And it will take a long time for that to be perfected, try getting this to work on it for example:

    Go here:https://example.com
     
    krt, Jul 26, 2007 IP
  4. chrisman826

    chrisman826 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks a million, thats just what i needed.
     
    chrisman826, Jul 26, 2007 IP