-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 :)

3

5 Answers 5

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Seems like overkill for what he wants, maybe I don't understand what he wants though.
He tagged it with php and preg-replace. I thought he wants to replace urls with links in some posted data.
1
$replaced = preg_replace('/(http[s]?:\/\/[^\s]*)/i', '<a href="$1">$1</a>', $url);

Comments

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;

?>

1 Comment

I think what he wants is to process an entire page and linkify urls.. the SO question linked from the comments has the answer.
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

Comments

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;
}
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.