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 Coder
    Join Date
    Dec 2009
    Location
    England
    Posts
    84
    Thanks
    1
    Thanked 11 Times in 11 Posts

    Regular Expressions - URL's that DON'T have a tag?

    Hello,

    Here is an example of the data I have:

    PHP Code:
    Test [url=http://yay.com]http://yay2.com[/url] [url]http://yay3.com[/url] [url]http://www.yay4.com[/url] 
    This is what I want the output to be:

    PHP Code:
    Test [url=http://yay.com]http://yay2.com[/url] <a href="http://yay3.com">http://yay3.com</a> <a href="http://www.yay4.com">http://www.yay4.com</a> 
    As you can see, I want the code to ignore the url's inside the [url] tags... I have this:

    PHP Code:
    function parse_urls($string)
    {
              
    $host "([a-z\d][-a-z\d]*[a-z\d]\.)+[a-z][-a-z\d]*[a-z]";
              
    $port "(:\d{1,})?";
              
    $path "(\/[^?<>\#\"\s]+)?";
              
    $query "(\?[^<>\#\"\s]+)?";
              
    $string preg_replace("#(?!url=)((ht|f)tps?:\/\/{$host}{$port}{$path}{$query})(?!\[\/url\])#i""<a href=\"$1\">$1</a>"$string);
        return 
    $string;

    But it does not seem to work correctly, it updates all the links, including the ones in the [url] tags. What am I doing wrong?

    Thanks.
    Last edited by AIOBB-Richard; 01-18-2010 at 05:58 AM. Reason: resolved
    Current Project: All-in-One Bulletin Board (AIOBB)
    Info: www.aiobb.com
    Demo: www.aiobb.com/forums

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    PHP Code:
    $string preg_replace('%\[url\](.*?)\[/url\]%''<a href="$1">$1</a>'$string); 
    That should do it
    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 :)

  • #3
    New Coder
    Join Date
    Dec 2009
    Location
    England
    Posts
    84
    Thanks
    1
    Thanked 11 Times in 11 Posts
    Quote Originally Posted by JAY6390 View Post
    PHP Code:
    $string preg_replace('%\[url\](.*?)\[/url\]%''<a href="$1">$1</a>'$string); 
    That should do it
    Nope, that does not work at all.
    Current Project: All-in-One Bulletin Board (AIOBB)
    Info: www.aiobb.com
    Demo: www.aiobb.com/forums

  • #4
    New Coder
    Join Date
    Dec 2009
    Location
    England
    Posts
    84
    Thanks
    1
    Thanked 11 Times in 11 Posts
    Hey,

    Just noticed that this forum parsed my link's and added url tags where there was not meant to be any.

    What I need, is a regex to pick up URL's NOT already in url tags. so
    PHP Code:
    [url=http://google.com/]http://google.com/[/url] 
    is NOT picked up, but http://google.com is when used not in a url tag.
    Current Project: All-in-One Bulletin Board (AIOBB)
    Info: www.aiobb.com
    Demo: www.aiobb.com/forums

  • #5
    New Coder
    Join Date
    Dec 2009
    Location
    England
    Posts
    84
    Thanks
    1
    Thanked 11 Times in 11 Posts
    Thanks for the help guys, decided to do it the long way;

    PHP Code:
        function parse_urls($string$rel 'nofollow')
        {
              
    $host "([a-z\d][-a-z\d]*[a-z\d]\.)+[a-z][-a-z\d]*[a-z]";
              
    $port "(:\d{1,})?";
              
    $path "(\/[^?<>\#\"\s]+)?";
              
    $query "(\?[^<>\#\"\s]+)?";
            
    preg_match_all("#((ht|f)tps?:\/\/{$host}{$port}{$path}{$query})#i"$string$pattsPREG_OFFSET_CAPTURE);
            
            
    $patts[0] = array_reverse($patts[0]);
            
            foreach (
    $patts[0] as $value)
            {
                if (
    strtoupper(substr($string$value[1] - 55)) != "[URL=")
                {
                    if (
    strtoupper(substr($string$value[1] + strlen($value[0]), 6)) != "[/URL]" )
                    {
                        
    // Replace away...
                        
    $string substr_replace($string"<a href=\"".$value[0]."\" target=\"_blank\" rel=\"{$rel}\">".$value[0]."</a>"$value[1], strlen($value[0]));
                    }
                }
            }
            
            return 
    $string;
        } 
    Current Project: All-in-One Bulletin Board (AIOBB)
    Info: www.aiobb.com
    Demo: www.aiobb.com/forums


  •  

    Posting Permissions

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