Welcome Guest, Not a member yet? Register   Sign In
Auto_link bug
#1

[eluser]Iverson[/eluser]
When entering a string with web address following by a comma, such as "If you type www.example.com, you'll see what I mean!" it will return

Quote:"If you type <a href="http://www.example.com,">http://www.example.com,</a> you'll see what I mean!"
#2

[eluser]xwero[/eluser]
You can't be certain a comma in the end of an url means the url ended, some sites use them. I think it's not a bug because it can't be avoided. Every parser will have his weak points.
#3

[eluser]Iverson[/eluser]
[quote author="xwero" date="1237836764"]You can't be certain a comma in the end of an url means the url ended, some sites use them. I think it's not a bug because it can't be avoided. Every parser will have his weak points.[/quote]

Agreed but if there isn't anything after the comma, the parser doesn't need to include it. Consider the fact that all major browsers append a trailing slash after a given url ("www.example.com" becomes "http://www.example.com/"). Ergo the parser returns a hyperlink that ends in ",/". It isn't a weakness because any commas from a site will be after the first trailing slash. Any comma before the first trailing slash is invalid 100% of the time thus should be taken into consideration in the regex. I'm in no way claiming to know the proper regex but I can't concede the fact that this isn't something that can be checked every time.
#4

[eluser]xwero[/eluser]
I've never seen a browser to append a forward slash at the end of an url?

But you are right a comma can't be added to a host name but then the code needs to be changed a lot. This is what works with a few tests i did.
Code:
foreach($matches[0] as $i=>$match)
        {
            $url = $match;
            $anchor = '';
            // remove first matched group because it is not a part of the url
            $url = str_replace($matches[1][$i],'',$url);

            if( ! in_array($matches[2][$i],array('http://','https://')))
            {
                $url =  'http://'.$url;
            }

            $url_arr =  parse_url($url);

            $url = $url_arr['scheme'].'://';

            if(isset($url_arr['user']))
            {
                $url .= $url_arr['user'].':'.$url_arr['pass'].'@';
            }
            
            if(preg_match('/\W$/',$url_arr['host']))
            {
              $url .= substr($url_arr['host'],0,-1);
              $after = substr($url_arr['host'],-1);
            }
            else
            {
               $url .= $url_arr['host'];
               $after = '';
            }

            if(isset($url_arr['path']) && strlen($url_arr['path']) > 1)
                {
                   $url .= $url_arr['path'];
                }

                

                if(isset($url_arr['query']))
                {
                   $url .= '?'.$url_arr['query'];
                }

                if(isset($url_arr['fragment']))
                {
                   $url .= '#'.$url_arr['fragment'];
                }

            $anchor = '<a href="'.$url.'">'.$url.'</a><br>'.$after;

            $str = str_replace($match,$anchor,$str);
        }
The foreach loop replaces the for loop in the fuction.
#5

[eluser]Iverson[/eluser]
[quote author="xwero" date="1237844990"]I've never seen a browser to append a forward slash at the end of an url?[/quote]

Thanks. Try simply typing "www.codeigniter.com" into your browser. You'll see the site and the address bar will read "http://www.codeigniter.com". Then type "www.codeigniter.com," and see whatcha get :bug:
#6

[eluser]xwero[/eluser]
that is a first for me, but i never typed urls with comma before Smile
#7

[eluser]Iverson[/eluser]
[quote author="xwero" date="1237847613"]that is a first for me, but i never typed urls with comma before Smile[/quote]

Me neither. I'm buidling a site that will accept a user's comments. I did exactly like in the example:

Quote:If you look at www.codeigniter.com, you'll see an example of ExpressionEngine

That's where the most popular situation will be. That is, if people exercise good grammar like I do :lol:
#8

[eluser]Iverson[/eluser]
FYI, I just tried the same sentence in Gmail, and it parsed it right. Maybe we should ask them Smile
#9

[eluser]xwero[/eluser]
I changed the host part line because it was removing the comma without preserving it, now it preserves the comma to add after the anchor tag.
#10

[eluser]sophistry[/eluser]
hey guys, looking over this old thread... thought I'd link you to my fix:

http://ellislab.com/forums/viewthread/95384/P15/

it fixes this 'comma at the end of the url' bug in auto-link as well as others - like auto_link() can find www.codeigniter.com, but not wiki.codeigniter.com




Theme © iAndrew 2016 - Forum software by © MyBB