Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Hyperlink URLS

  1. #1
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts

    Hyperlink URLS

    I've got this function:

    PHP Code:
    /* clickable($url) changes a block of text to use urls */
    function clickable($url){
     
    $in = array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`si','`((?<!//)(www\.\S+[[:alnum:]]/?))`si');
      
    $out = array('<a href="$1" rel="nofollow" target="_blank">$1</a>''<a href="http://$1" rel="nofollow" target="_blank">$1</a>');
       return 
    preg_replace($in,$out,$url);

    And, I just tried it out with a series of other functions, and it went haywire. I don't understand regex yet, so help is appreciated ^.^.

    Here's the context I'm using it in:

    PHP Code:
    /* escape_data($string) This function cleans data for databases. */

    function escape_data ($data) {
    db_connect();
     if (
    ini_get('magic_quotes_gpc')) {
      
    $data stripslashes($data);
     }
      
    $data mysql_escape_string (trim($data));
    db_close();
       return 
    $data;
    }

    /* format_string($str) fixes a string for input and display */
    function format_string($str) {
     
    $string escape_data($str);
      
    $string str_replace("\n""<br />"$string);
       
    $string clickable($string);
         return 
    $string;
    }

    echo 
    format_string($input); 

    The problem I was having was rather odd. The hyperlink linked multiple url strings with 1 anchor. But it did that twice. The format_string function also failed to replace '\n' with '<br />' in the instance when I used clickable(). It's never done that before, though =/.

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    If you're working with URL's, I highly recommend using parse_url() instead of a regex
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

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