1 2 3 4 5 6
<? php if $userinput = "http://www." { echo "<a href=\"URL\">URL</a>"; } ?>
Refactorings
No refactoring yet !
Mlopez
January 2, 2008, January 02, 2008 01:40, permalink
im not quite sure what to do or how to do it. if any one knows please help me out.
thanks.
timmeh
January 2, 2008, January 02, 2008 03:18, permalink
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php function URLref($sentence){ $temp = explode(" ", $sentence); $new = ""; foreach($temp as $i){ if(preg_match('([A-Za-z][A-Za-z0-9+.-]{1,120}:[A-Za-z0-9/](([A-Za-z0-9$_.+!*,;/?:@&~=-])|%[A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)', $i)){ $new .= '<a href="'.$i.'">'.$i.'</a>'; }else{ $new .= "$i "; } } return trim($new); } $sentence = "My site ULR is http://www.google.com/lolz.html"; echo URLref($sentence);
Mlopez
January 2, 2008, January 02, 2008 15:41, permalink
ok, now the only problem is that if a user just enter "http://www.example.com" the way my script runs, it enter a <br /> after each line of input. and when a link is entered it displays it a as
ex.
http://www.example.com
/>
and the link is "http://www.example.com<br"
how can i fix this?
Mlopez
January 2, 2008, January 02, 2008 15:43, permalink
also doing the url first then adding a space and putting text make the url functional but it cuts out the space and make the text right next to the link text with no space...
Jeremy Weiskotten
January 2, 2008, January 02, 2008 23:21, permalink
Some examples of PHP linkifiers.
http://www.thescripts.com/forum/thread10857.html
http://www.koders.com/php/fid2A4EBD1DDCDEAC78867AB44B5805EAE7FAEA339B.aspx
shtruc
January 8, 2008, January 08, 2008 22:35, permalink
If i understand correctly, the idea is to take some user input and create <a> elements for each valid url in it.
Try this regexp :
1 2 3
<?php $input = preg_replace("/(www\.|http:\/\/)([^\s]+)/", '<a href="http://$1$2" target="_blank">$1$2</a>', $input); ?>
Lukio
March 3, 2008, March 03, 2008 18:18, permalink
if I want to see only name of the domain in the link?
ex.
url = 'http://www.site.com';
<a href="http://www.site.com">site</a>
bec
June 29, 2008, June 29, 2008 02:58, permalink
This snippet corrects a slight syntax error in shtruc's snippet (link hrefs would be reproduced as "http://http://example.com"). It assumes that URLs are not already in link tags (you've cleaned any html a user may have entered beforehand), and also takes the liberty of trimming the link text to 68-ish characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php $input = <<<EOF http://www.example.com/ http://example.com www.example.com http://iamanextremely.com/long/link/so/I/will/be/trimmed/down/a/bit/so/i/dont/mess/up/text/wrapping.html EOF; $output = preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,68})[^\s<]*)/", '<a href="http://$2$3" rel="nofollow">$2$4</a>', $input); /* This results in the following links: <a href="http://www.example.com/" rel="nofollow">www.example.com/</a> <a href="http://example.com" rel="nofollow">example.com</a> <a href="http://www.example.com" rel="nofollow">www.example.com</a> <a href="http://iamanextremely.com/long/link/so/I/will/be/trimmed/down/a/bit/so/i/dont/mess/up/text/wrapping.html" rel="nofollow">iamanextremely.com/long/link/so/I/will/be/trimmed/down/a/bit/so/i/do</a> */ ?>
Andreas
November 6, 2008, November 06, 2008 20:44, permalink
Great! bec, this was EXACTLY what I was looking for! Easy, smooth and safe :). Thanks!
Andreas
November 6, 2008, November 06, 2008 21:11, permalink
Actually I found one issue ... if the url is ending with a "dot" or "comma", e.g.: visit www.example.com, now.
The comma messes things up ... as it becomes http://www.example.com,/
Is there a solution to "characters in the end of the url"? It's all that lacks in this excellent snippet ...
Thanks!
Andreas
Skip
October 12, 2009, October 12, 2009 21:35, permalink
Hi there,
Just stumbled upon this thread and hoping somone can help me out. I have the oposite problem. I need to remove the code from around the url which is coming out of the db.
I need to be left with only the www.asdf.com towards the end of the line of code between the "> and the < /a>
Any ideas how to do this?
Thanks
1
<a href="http://www.asdf.com" target="_blank">www.asdf.com</a>
Paul
January 20, 2010, January 20, 2010 16:49, permalink
To not include comma's just need to change the the end of url from \s to (\s|,) like this:
1 2
$output = preg_replace("/(http:\/\/|(www\.))(([^(\s|,)<]{4,68})[^(\s|,)<]*)/", '<a href="http://$2$3" rel="nofollow">$2$4</a>', $input);
ucorp
July 16, 2010, July 16, 2010 19:24, permalink
is there anyway to test this against XSS attacks?
im not quite sure where to start. i want to have a php script that looks at a users input and looks for a valid link (ex. http://www.example.com) and if url is in that text replace it with <a href="URL">URL</a>