132a00e84e74c003f68311b39b28e969

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>

1
2
3
4
5
6
<? php
if $userinput = "http://www."
{
      echo "<a href=\"URL\">URL</a>";
}
?>

Refactorings

No refactoring yet !

132a00e84e74c003f68311b39b28e969

Mlopez

January 2, 2008, January 02, 2008 01:40, permalink

No rating. Login to rate!

im not quite sure what to do or how to do it. if any one knows please help me out.

thanks.

Avatar

timmeh

January 2, 2008, January 02, 2008 03:18, permalink

2 ratings. Login to rate!
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);
132a00e84e74c003f68311b39b28e969

Mlopez

January 2, 2008, January 02, 2008 15:41, permalink

No rating. Login to rate!

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?

132a00e84e74c003f68311b39b28e969

Mlopez

January 2, 2008, January 02, 2008 15:43, permalink

No rating. Login to rate!

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...

5170ca260dbd2cdfd5a887a4dba7636f

Jeremy Weiskotten

January 2, 2008, January 02, 2008 23:21, permalink

No rating. Login to rate!
C2eee2044eac9c9b7f7d952bd3a7c6fc

shtruc

January 8, 2008, January 08, 2008 22:35, permalink

3 ratings. Login to rate!

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);
?>
70bf853d7968e44fab3f9edb2e43fa3d

Lukio

March 3, 2008, March 03, 2008 18:18, permalink

No rating. Login to rate!

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>

0e3ff8f33031759d310a7223009d031b

bec

June 29, 2008, June 29, 2008 02:58, permalink

1 rating. Login to rate!

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>

*/

?>
Bb4412496d1dd0d39ff38af6bc01dace

Andreas

November 6, 2008, November 06, 2008 20:44, permalink

No rating. Login to rate!

Great! bec, this was EXACTLY what I was looking for! Easy, smooth and safe :). Thanks!

Bb4412496d1dd0d39ff38af6bc01dace

Andreas

November 6, 2008, November 06, 2008 21:11, permalink

No rating. Login to rate!

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

Avatar

Skip

October 12, 2009, October 12, 2009 21:35, permalink

No rating. Login to rate!

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>
9d73224bb10f49c92983d0492ce9cc17

Paul

January 20, 2010, January 20, 2010 16:49, permalink

No rating. Login to rate!

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);
94b511efe3ee9cb76bdf58f8f6b32756

ucorp

July 16, 2010, July 16, 2010 19:24, permalink

No rating. Login to rate!

is there anyway to test this against XSS attacks?

Your refactoring





Format Copy from initial code

or Cancel