Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Parse a string, turning URL's into links

Last post 07-02-2009, 12:38 PM by ddrudik. 7 replies.
Sort Posts: Previous Next
  •  06-29-2009, 11:01 AM 54425

    Parse a string, turning URL's into links

    Very good at PHP, but i've always avoided text manipulations or anything to do with regular expressions as it's my down fall, but i recently i've been trying to learn.

     

    I just need some help with a regular expression to find all URL's in a user inputed string

    http://kieranpeat.co.uk

    would turn into

    [link:http://kieranpeat.co.uk]kieranpeat.co.uk[/link]

    or

    www.kieranpeat.co.uk/blog.php?id=1345

    into

    [link:www.kieranpeat.co.uk/blog.php?id=1345]kieranpeat.co.uk / blog[/link]

    how easy would the last bit be ? (the part where it strips off the '.php?id=12345' and just displays the page name

    Filed under:
  •  06-29-2009, 11:08 AM 54426 in reply to 54425

    Re: Parse a string, turning URL's into links

    Could someone enter [link:...] format instead of a bare link?
  •  06-29-2009, 9:14 PM 54434 in reply to 54426

    Re: Parse a string, turning URL's into links

    Well it was just as an example using BB instead of HTML, some of the projects i'd turn into BB, others HTML.

    Also this is going to be turn any user inputed data on a community site into a link, alot of people don't bother using either HTML or BB code, just put the URL.

    It'd also be good for me to analyse it for learning

     

    Kieran

  •  07-01-2009, 10:53 AM 54474 in reply to 54425

    Re: Parse a string, turning URL's into links

    Right well i'm now using

    function link ($string)
    {
        $pattern  = '|([A-Za-z]{3,9})://';     // protocol
        $pattern .= '([-;:&=\+\$,\w]+@{1})?';  // User info (optional)
        $pattern .= '([-A-Za-z0-9\.]+)+';      // FQDN or IP
        $pattern .= ':?(\d+)?';                // port (optional)
        $pattern .= '(';                       // start to capture complete REQUEST_URI
        $pattern .= '(/[-\+~%/\.\w]+)?';       // path and filename (optional)
        $pattern .= '\??([-\+=&;%@\.\w]+)?';   // query string (optional)
        $pattern .= '#?([\w]+)?';              // anchor (optional)
        $pattern .= ')?|';                     // end capturing REQUEST_URI and close pattern
        return preg_replace($pattern, '<a href="$0" target="_blank">$0</a>', $string);
    }

    Only thing that i've tested that it won't match it things that don't start with http:// but start with www.

    Not sure how to change it, and will it affect the performace ?

  •  07-02-2009, 11:37 AM 54508 in reply to 54474

    Re: Parse a string, turning URL's into links

    I've also got a BB code function going on.

     But, they get in get others way and if i post

     

     then it will also find that and create it into a link.

     

    I tried to play around with the protocol part to get it to not accept anything that has [img] or [img=] before it, but i couldn't get it to work, any pointers ?

  •  07-02-2009, 11:56 AM 54511 in reply to 54426

    Re: Parse a string, turning URL's into links

    ddrudik:
    Could someone enter [link:...] format instead of a bare link?

    I asked this question to clarify if the solution needs to be concerned with avoiding existing [link:...] blocks when locating URLs to convert into [link...]s.


  •  07-02-2009, 12:04 PM 54517 in reply to 54511

    Re: Parse a string, turning URL's into links

    Thought you were calling me lazy more than anything ;P

    At that time i didn't have BB code, but now i've been learning abit more about regular expressions i put some basic BB code in, but it does conflict with the regular expression to find URL's, which is way above my head.

  •  07-02-2009, 12:38 PM 54518 in reply to 54517

    Re: Parse a string, turning URL's into links

    I will leave it to you to find a suitable URL match pattern (regexlib.com offers a number of match patterns), but you can consider this general replacement method:

    <pre>
    <?php
    $sourcestring='http://kieranpeat1.co.uk
    http://kieranpeat2.co.uk/test/test.htm
    http://www.kieranpeat.co.uk/blog.php?id=1345'
    ;
    echo $sourcestring.'<hr>';
    $matchpattern='~https?://([^\s?]+)\S*~i';
    function repfunc($match){
            return '[link:'.$match[0].']'.$match[1].'[/link]';
    }
    $sourcestring=preg_replace_callback($matchpattern,'repfunc',$sourcestring);
    echo $sourcestring;
    ?>


View as RSS news feed in XML