-1

Possible Duplicate:
replace any url's within a string of text, to clickable links with php

Just a quick question, When I post links like http://www.buddyweb.me it will just appear like that but, but t's not automaticly linked. So how can I replace the http://www.buddyweb.me with <a href = "http://www.buddyweb.me">Google</a>

Any suggestions are apreciated, thanks :)

Community
  • 1
  • 1
frankmeacey
  • 151
  • 1
  • 3
  • 8
  • 3
    http://stackoverflow.com/questions/1798912/replace-any-urls-within-a-string-of-text-to-clickable-links-with-php – Rijk Sep 20 '11 at 20:49
  • Please clarify whether what you want is to process a page and turn urls into links or just echo a link from php? – 0x6A75616E Sep 20 '11 at 20:52
  • The question linked by @Rijk, links to: http://stackoverflow.com/questions/1038284/php-parse-links-emails – feeela Sep 20 '11 at 20:56

5 Answers5

1

Jut like here

function clickable($url){
    $url                                    =    str_replace("\\r","\r",$url);
    $url                                    =    str_replace("\\n","\n<BR>",$url);
    $url                                    =    str_replace("\\n\\r","\n\r",$url);

    $in=array(
    '`((?:https?|ftp)://\S+[[:alnum:]]/?)`si',
    '`((?<!//)(www\.\S+[[:alnum:]]/?))`si'
    );
    $out=array(
    '<a href="$1"  rel=nofollow>$1</a> ',
    '<a href="http://$1" rel=\'nofollow\'>$1</a>'
    );
    return preg_replace($in,$out,$url);
}
NiematojakTomasz
  • 2,433
  • 20
  • 23
1
$replaced = preg_replace('/(http[s]?:\/\/[^\s]*)/i', '<a href="$1">$1</a>', $url);
benck
  • 2,034
  • 1
  • 22
  • 31
0

No need for preg-replace, just concatenate varibles around your link.

<?

$yourlink = "http://www.buddyweb.me";
$yourDescriptor = "Google";

$linkedlink = "<a href=\"".$yourlink.">$yourDescriptor</a>";

echo $linkedlink;

?>
Kevin Collins
  • 332
  • 1
  • 12
  • I think what he wants is to process an entire page and linkify urls.. the SO question linked from the comments has the answer. – 0x6A75616E Sep 20 '11 at 20:51
0
echo preg_replace("#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is", "\\1<a href=\"\\2\" title=\"\\2\" rel=\"nofollow\">\\2</a>", $string);

I would consider this a complicated Regular Expression. However, if you're interested in learning more, I really liked getting started with this video http://www.youtube.com/watch?v=DRR9fOXkfRE

Morgan Delaney
  • 2,349
  • 3
  • 20
  • 23
0

Call something that will return it how you like.

<?php
$link = "http://stackoverflow.com";
$name = "Stack Overflow";

echo href($link, $name);

function href($link, $name){
$link = "<a href=\"".$link.">$name</a>";
return $link;
}
?>
dreuv2
  • 23
  • 3