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

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

*/

?>

Your refactoring





Format Copy from initial code

or Cancel