Welcome Guest, Not a member yet? Register   Sign In
auto_link with 2 urls from the same domain
#22

[eluser]dakdad[/eluser]
Hi All,

Here is another workaround to fix this problem.

I have introduced another function to compare the length of the matched strings and sort the matched array by longest match to the shortest match.

So, when replacing the text with links, it would replace the lengthy ones first so the smaller matches that are actually part of a bigger match does not replace it.

Here is the code http://pastebin.com/m119e8589

Hope this helps

Cheers
#23

[eluser]sophistry[/eluser]
hi dakdad, welcome to CI.

please note that the code you posted sorts the $matches[0] array but does not sort the corresponding $matches[n] arrays. therefore it breaks the required correspondence between those arrays in the (definitively broken) "blind" str_replace(). so, the links are all applied backwards.

further, even though the code could sort all sub-arrays using array_multisort() to keep them in correspondence it would still suffer from other problems identified in the current auto_link() code.

specifically, the "blind str_replace()" problem still persists. this is because the string variable $str is replaced OVER the previous $str variable. but, also the arbitrary choice of www. as a "hint" to tell the function to "linkify" the string is plain wrong - there are many many many URLs that do not use www. but still lead to websites: del.icio.us for one. there are other problems detailed in this thread and testable in code posted.

if you'd like, you could test that new code that has already had several hours of debugging and testing work put into it. :-)

cheers.
#24

[eluser]dakdad[/eluser]
[quote author="sophistry" date="1247694265"]
please note that the code you posted sorts the $matches[0] array but does not sort the corresponding $matches[n] arrays. therefore it breaks the required correspondence between those arrays in the (definitively broken) "blind" str_replace(). so, the links are all applied backwards.
[/quote]

Oh .. thank you so much for pointing that out.

I have to use your code that works.

I obviously have not been thorough with my testing. However, I do believe that there is a simpler solution for this ... I will continue to research this .. few more coffees may be.
#25

[eluser]sophistry[/eluser]
ok, sounds good. i look forward to coffee-fueled insights!

cheers.
#26

[eluser]dakdad[/eluser]
[quote author="sophistry" date="1247722947"]ok, sounds good. i look forward to coffee-fueled insights![/quote]

Okay, I have to admit - there is no easy way of doing this, not if you want to link things like del.icio.us and various sub domains. It can be easier if you are only looking for "www." or "http" start.

So, now I'm using the code you published and it works great, except for one small problem. In this particular instance I do not want the emails to be linked.

But the code links the ending domain of the email address.

Code:
So I get info@<a href="http://example.com">example.com</a>example.com</a>

I have not found a solution yet, but will keep looking.
#27

[eluser]dakdad[/eluser]
Okay, this workaround works for me
Code:
$p = '#(?<=\s)(?!['.$not.'])(https?://)?(?!['.$not.'])([-[:alnum:]]++[.]([[:alnum:][:punct:]]+)?)(?<!['.$not.'])#i';

The regex looks for a space before the start of the match.

PS. I have not done any extensive testing, but so far I have not seen any errors within my app.
#28

[eluser]sophistry[/eluser]
interesting.

i think you could just put a space in that regex... i don't think you need that "positive lookbehind assertion" to make sure a space starts the domain name string (unless you are doing something fancy).

the reason i didn't do that was to be able to catch enjambed http strings that for one reason or another were not separated by a space. (see code comments for rationale).

cheers.
#29

[eluser]Unknown[/eluser]
Any chance this can be easily updated to ignore URLs that are already enclosed in <a> tags?
#30

[eluser]sophistry[/eluser]
no, not easily. Wink




Theme © iAndrew 2016 - Forum software by © MyBB