Forum Moderators: phranque

Message Too Old, No Replies

change text urls only

do not parse html urls

         

phparion

9:24 am on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi

I have been facing problems in making text urls, e.g (www.example.com), a click-able URL on the fly. The problem is that i have text urls as well s proper html urls in my html. when i try to convert text urls to html links, the already proper html links are disturbed too and vice versa.

I have tried the following kind of patterns,

// $text = eregi_replace('[^<]([[:space:]()[{}])?(((f¦ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)([[:space:]()[{}])', ' <a href="\\3" target=\"_blank\">\\3</a> ', $text);
//$text = eregi_replace('[^<a]([[:space:]()[{}])?(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)([[:space:]()[{}])?', '\\1 <a href="http://\\2" target=\"_blank\">\\2</a> ', $text);
//$text = eregi_replace('([[:space:]()[{}])?(file:(/)?[-a-zA-Z0-9@:%_\+.~#?&//=]+)([[:space:]()[{}])?', '\\1 <a href="\\2">\\2</a> ', $text);
//$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\2</a>', $text);

//$text = eregi_replace('(((f¦ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)','<a href="\\1" target=_blank>\\1</a>', $text);

//$text=preg_replace( "/[^(<a href)]((http¦ftp)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\">\\0</a>", $text );
//$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)','\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
//$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $text);

$text = preg_replace('\b(https?¦ftp¦file):\/\/[-A-Z0-9+&@#\/%?=~_¦!:,.;]*[-A-Z0-9+&@#\/%=~_¦]', '<a href="\\0">\\0<\/a>', $text);

return $text;

none of them is working for me.

Can anybody help please how to accomplish this?

thank you very much

coopster

1:51 pm on May 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



phparion, same issue as here [webmasterworld.com] yet? I didn't test that regex you threw down, but if that is not working, where or on what type of text string is it failing?

phparion

8:17 pm on May 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



yeah, that was a false wave of joy :).. it works for converting text url to html but doesn't leave html urls AS IS. e.g when i post two urls as follow

-------------------------------
www.example.com lots of html crap and other things then <a href="http://example.com/somepage.html">link</a> again other text and similar urls
------------------------------------------------------

then the text urls are converted to clickable links but html urls are broken like

href="http://example.com/somepage.html">link</a>

the starting <a is disappeared.

i am using the same preg_replace as i posted in the other thread with negative assertion.

any help is much appreciated. you can test your regex with both types of links in single string to see the affect.

coopster

2:56 pm on May 6, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK. I'll give things a shot and reply in the PHP thread.

phparion

6:37 pm on May 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@Coopster: no reply in apache or php room :) not pushing you just reminding :P

TheMadScientist

7:08 pm on May 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you be very specific about what you are trying to convert, and what you are trying to not touch?

Will the URLs you would like to match to 'make clickable' always be parenthasized?
Will they all be domain only, or will there be paths also?

Please give a couple of examples.

coopster

7:53 pm on May 9, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Some examples would be good. I responded in the other thread. I'm guessing that the reason it isn't working (from looking at some of your other regexs up there) is that you are also attempting to locate barenaked
www.example.com
links within the text. We should keep our responses in one thread or the other though, so we don't create confusion amongst ourselves or future readers.

phparion

9:48 am on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



sorry for late reply, I had fallen sick :(

anyway, I will keep future posts on this problem here in apache as it is more of regex question than php.

my goal is very simple,

1 - if a url is present in the text form then make it clickable e.g

www.example.com example.com http://example.com/page.ext http://www.example.com/directories/pages/

2 - if proper html links are present then do not touch it at all e.g all above links with <a href=''></a> tags.

I have tried uncounted regexs but am not able to solve this. If it works for text url then breaks html urls and if does not break html then text urls are not converted.

killahbeez

10:55 am on May 15, 2008 (gmt 0)

10+ Year Member



preg_replace("@(http://[\w\./-]+)(?=([^><]*)<)@i","<a href='$1'>$1</a>",$content);

phparion

6:11 pm on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



it doesn't convert text urls to clickable.