Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: eregi_replace; recognize hyperlinks automatically

  1. #11
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Been busy with other things - I haven't tried out all of your options above - some of them created problems. I figured out this method which works fairly well:
    PHP Code:
    $text "
    Here is a text - www.ellehauge.net - it has some links with e.g. comma, www.one.com, in them.
    Some links look like this: http://mail.google.com - mostly they end with a space or 
    carriage return www.unis.no
    <br /> - but they may also end with a period: http://ellehauge.net. You may even put 
    the links in brackets (www.skred-svalbard.no) (http://one.com).
    From time to time, links use a secure protocol like https://gmail.com - tricky, isn't it?
    "
    ;

    echo 
    '<p>'.$text.'<br />&nbsp;<br /></p>';

    // anything which starts with "http" and has URL chars
    $text eregi_replace(
      
    'http([-_./a-z0-9!&%#?+,\'=:;@~]+)',
      
    '<a href="http\\1" rel="ext">http\\1</a>'$text);
    // anything which starts with "www." and has URL chars
    $text eregi_replace(
      
    'www.([-_./a-z0-9!&%#?+,\'=:;@~]+)',
      
    '<a href="http://www.\\1" rel="ext">www.\\1</a>'$text);
    // remove "," from the end of hrefs
    $text=str_replace('," rel="ext">','" rel="ext">',$text);
    // remove "." from the end of hrefs
    $text=str_replace('." rel="ext">','" rel="ext">',$text);

    echo 
    '<p>'.$text.'</p>'
    - except that I still get hyperlinks where the "," and "." still APPEAR within the link - although it has been removed from the link itself. I.e. technically it works - but it doesn't look great. Any ideas how to get the comma/period out of the text?

    I might try out some of your solutions above if they proved to work?

    NB: Ignore the <<rel="ext">> - it has something to do with some javascripting to avoid using the depricated "target:_blank" ...
    Last edited by jonas-e; 11-06-2007 at 12:52 PM.

  2. #12
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking

    Yo - I seem to have found a way which is nearly fool proof:
    PHP Code:
    $text "
    Here is a text - www.ellehauge.net - it has some links with e.g. comma, www.one.com, 
    in it. Some links look like this: http://mail.google.com - mostly they end with a 
    space or carriage return www.unis.no
    <br /> - but they may also end with a period: http://ellehauge.net. You may even put 
    the links in brackets (www.skred-svalbard.no) (http://one.com).
    From time to time, links use a secure protocol like https://gmail.com |
    This.one.is.a.trick. Sub-domaines: http://test.ellehauge.net | 
    www.test.ellehauge.net | Files: www.unis.no/photo.jpg | 
    Vars: www.unis.no?one=1&amp;~two=2 | No.: www.unis2_check.no/doc_under_score.php |
    www3.one.com | another tricky one:
    http://ellehauge.net/cv_by_id.php?id%5B%5D=105&amp;id%5B%5D=6&amp;id%5B%5D=100.<br />
    Here is my [link=blog.php]blog&raquo;[endlink] <br />
    This one contains http AND www: http://www.unis.no
    "
    ;
    echo 
    '<p style="text-align:left;">'.$text.'</p></div>';

    function 
    hyper($text) {
        
    // replace those starting with http(s)
        
    $text preg_replace(
            
    '~((https?)(://[-/a-zA-Z0-9_\?=&;:#%!@\~]+)(\.[-/a-zA-Z0-9_\?=&;:#%!@\~]+){1,})~',
            
    '<a href="\1" rel="ext">\1</a>'$text);
        
    // replace those starting with www (but NOT http://www!)
        
    $text preg_replace(
            
    '~(\s|\()((www[0-9]?)(\.[-/a-zA-Z0-9_\?=&;:#%!@\~]+){1,})~',
            
    '\1<a href="http://\2" rel="ext">\2</a>'$text);
        
    // links within the same site
        
    $text eregi_replace(
            
    '\\[link=([-_./a-zA-Z0-9!&%#?+,\'=:;@~]+)]([^\\[]+)\\[endlink]',
            
    '<a href="\\1">\\2</a>'$text); // internal links - NOT http or www!
        
    return $text;
    }

    $content=hyper($text);
    echo 
    '<p>'.$content.'</p>'
    There might still be some falts in it - let me know if you find any ...

  3. #13
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    All the regular expressions above seem to forget that a site doesn't have to have www in it. I could setup an addresss to be something like https://myforums.sweetsite.co.uk . That address is perfectly valid. I threw the following expression together real quick.
    PHP Code:
    <?php
    $test 
    "
    www.ebay.com
    www.somesite.com
    somesite.co.uk
    http://thiis9cool.nr
    https://www2.coolhuh.com. Interesting Huh?
    "
    ;
    $test_with_links preg_replace("/(http(s?):\/\/)?([a-zA-Z0-9\.]+\.[a-zA-Z]{2,3})/",'<a href="http$2://$3">http$2://$3</a>',$test);
    ?>
    I included my test text, just to show what it should handle.

  4. #14
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi blm126
    I just tested my function on your example https://myforums.sweetsite.co.uk - it works fine - due to the {1,} part.
    (://[-/a-zA-Z0-9_\?=&;:#%!@\~]+)
    - defines the first part after the http(s) starting with "://"
    (\.[-/a-zA-Z0-9_\?=&;:#%!@\~]+){1,}
    - let's you have as many ".***" elements as you like after that.
    (\s|\()
    - in front of "((www[0-9]?)" prevents expressions like "http(s)://www.**" from being processed twice - otherwise giving odd results ...

  5. #15
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    I hope I am not repeating what Jonas-e just said, but is there a way to modify the code to incorporate
    Code:
    echo preg_replace("#(<a\s[^>]+>http://\S+</a>)|(<[^>]+http://[^>]+>)|http://\S+#ie",'"$0"=="$1" || "$0"=="$2" ? "$0" : "<a href=\"$0\">$0</a>"',$text);
    into blm126's expression?

    I have been away from coding for a bit, but the above code will hyperlink urls and ignore the ones that are used for image src and ones that are already hyperlinked. It does not hyperlink ones that do not have the http:// prefix however, but then again that might be a good thing too. I found this example at http://www.tote-taste.de/X-Project/regex/eval.html.
    Last edited by james438; 11-16-2007 at 03:22 AM.

  6. #16
    Join Date
    Sep 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I have a question about this...

    I tried to figure it out but there doesn't seem to be any documentation I could find within the last hour pertaining to this.

    I have an internal script that takes user input which can include internal network URLs to a share on the network like "\\fileserver\file\spreadsheet.xls". I cannot for the life of me figure out how to write the preg_replace parameters to get it to find the link then replace with the appropriate hyperlink tag. Has anyone done this? Can it be done? I always end up stripping the leading \.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •