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> */ ?>
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>