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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Regex: parse multiple URLS as links

    I am trying to automatically parse URLS as a link in a text. It works with the script below. But the it only parses one link, not multiple. I am banging my head against the wall for hours now to solves this problem, can anyone help?


    Code:
    // The Regular Expression filter
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    
    // The Text you want to filter for urls
    $text = "This is my note with a link: https://domain.com/application.php?i=VHubsKu2ly7EfHXVLhEe# and with another link http://domain.com";
    
    // Check if there is a url in the text
    if(preg_match($reg_exUrl, $text, $url)) {
    
           // make the urls hyper links
           echo preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow">link ></a>', $text);
    
    } else {
    
           // if no urls in the text just return the text
           echo $text;
    
    }

  • #2

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Gvre for your reply.

    I changed it to preg_match_all but it doesn't work as expected, the urls are not parsed properly.
    Do you have any hints?

    PHP Code:
    $Note "This is my note with a link: https://domain.com/application.php?i=VHubsKu2ly7EfHXVLhEe# and with another link http://domain.com";


    $reg_exUrl "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    $stripped preg_match_all($reg_exUrl$Note$matchesPREG_SET_ORDER);

    foreach (
    $matches as $val) {
            
    $Note preg_replace($reg_exUrl'<a href="'.$val[0].'">link </a>'$Note);

    echo 
    "$Note"

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    241
    Thanks
    1
    Thanked 57 Times in 56 Posts
    You could use something like this

    PHP Code:
    $Note "This is my note with a link: https://domain.com/application.php?i=VHubsKu2ly7EfHXVLhEe# and with another link http://domain.com and this is another url http://www.foo.com/bar";

    $pattern '#((?:http|ftp)s?://\S+)#i';
    if (
    preg_match_all($pattern$Note$matches))
    {
            foreach (
    $matches[1] as $val)
                    
    $Note str_replace($val"<a href=\"$val\">link</a>"$Note);
            echo 
    $Note;


  • Users who have thanked gvre for this post:

    Jeru (02-12-2012)

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes, that's it!
    Thanks gvre for your input, it's greatly appreciated!


  •  

    Tags for this Thread

    Posting Permissions

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