0

Possible Duplicate:
How do I linkify urls in a string with php?

It would be so delightful if I could overcome this problem once and for all.

I need to able to create urls from strings like http://www.google.com and also www.google.com

function hyperlink($text)
{
    // match protocol://address/path/
    $text = ereg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $text);

    // match www.something
    $text = ereg_replace("(^| )(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\">\\2</a>", $text);

    // return $text
    return $text;
}
Community
  • 1
  • 1
Alexander
  • 309
  • 1
  • 4
  • 17
  • 1
    PERFECT! i cant even write what i want to becouse stachoverflow wont let me write urls, perfect! – Alexander Mar 13 '11 at 12:47
  • 3
    if you actually bothered searching SO before asking that question (like you have been asked to do in [Ask Advice](http://stackoverflow.com/questions/ask-advice)) you could happily copy and paste the solution from the [many existing answers to the questions your question is a duplicate of](http://stackoverflow.com/search?q=linkify+[php]). – Gordon Mar 13 '11 at 13:06

4 Answers4

1

You will find many good answers in the php manual. Although the examples are mainly on this page, you should use preg_replace instead.

$text = preg_replace('![a-z]+://[a-z0-9_/.-]+!i', '<a href="$0">$0</a>', $text);
$text = preg_replace('!(^| )(www([a-z0-9_/.-]+)!i', '$1<a href=\"http://$2\">$2</a>', $text);

Note: with preg you can use arbitrary delimiters, not just the standard / at the start and end of your expression. I used ! as it does not appear in the expression and this way you don't have to escape /. Also note that the i makes the expression case-insensitive so a-z is enough instead of a-zA-Z.

vbence
  • 20,084
  • 9
  • 69
  • 118
  • preg_replace instead of what? i do use preg replace but i cant use it on extreme regexp like i just posted above becose they comment out my code. – Alexander Mar 13 '11 at 12:43
  • Instead of what the original link points to (ereg). As I wrote in my answer I linked ereg for the comments, and advised to use preg in your production enviroment. – vbence Mar 13 '11 at 13:40
  • Also edited the answer for using preg. – vbence Mar 13 '11 at 13:46
0

Alex: so let me get this right, you've got a string, be it anything, and you wish to convert all instances of a URL to a link with the URL within it?

What i dont get is that you've already got it working with the regex you're trying:

$string = "
    <p>This string has http://www.google.com/ and has www.google.com it should match both</p>
";
$string = preg_replace("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i","<a href='$0'>$0</a>", $string);

echo $string;

will convert both URLs to be links as expected. I've not made any changes.

I'm thinking i'm missing what you mean, maybe you could paste some errors so we can see what the problem really is.

Andy
  • 679
  • 2
  • 10
  • 25
  • Except the `href='www.google.com'` isn't valid. – alex Mar 13 '11 at 13:10
  • what i mean is i want to be able to convert www.google.com string to "$0" but how do i know when to include the "http://" inside the href='' or not, i dont know how to do a conditional replacement, if it matches a string starting withj http:// then dont include http:// in the href, if it stats with www. incude, understand my problem? ^^ also i dont understand how ppl want me to use daring fireballs regex, i cant even copy paste it into my code without it commenting out my surrounding code and printing out errors.. – Alexander Mar 13 '11 at 13:10
  • you are right both strings gets converted to urls, but the last noe has a totally wrong href as you can see.. – Alexander Mar 13 '11 at 13:12
  • Alex, i've looked a little deeper and you're right, i missed the actually link :) Hmm, is there a specific reason why you're doing this entirely within regex? I get it could be faster, but it seems at the moment it's clearly not the right solution for your needs. I'm currently thinking of a different way to do this using the same REGEX but using a loop and str_replace. When i have something i'll post it up. Give me 2 secs and i'll see about getting it working. – Andy Mar 13 '11 at 13:17
  • i updated my post , check it out – Alexander Mar 13 '11 at 13:21
  • this function will convert anything, first the protocoll:// then only the www. :) – Alexander Mar 13 '11 at 13:23
  • Cool, so you got it working? As my approach started getting messy :) – Andy Mar 13 '11 at 13:35
0
function hyperlink($text)
{
    // match protocol://address/path/
    $text = ereg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $text);

    // match www.something
    $text = ereg_replace("(^| )(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\">\\2</a>", $text);

    // return $text
    return $text;
}
Alexander
  • 309
  • 1
  • 4
  • 17
0

I've just tested your solution and it works a treat, except for when you have a query string. e.g. www.example.com/search.php?q=ipod+nano&something=nothing will not get translated correctly.

I've made the relevant changes to your function below, this should now work more consistantly

function hyperlink($text)
{
    // match protocol://address/path/
    $text = ereg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $text);

    // match www.something
    $text = ereg_replace("(^| )(www([.]?[a-zA-Z0-9_/-\?&=\+%])*)", "\\1<a href=\"http://\\2\">\\2</a>", $text);

    // return $text
    return $text;
}

Just so you know i added: \?&=+% to the second regex.

You should test this across many more URL combinations.

But this should suffice for now.

Andy
  • 679
  • 2
  • 10
  • 25
  • you are right, thanks :) alltho there is probably less chance of someone posting an url of that magnitude without the http:// in front of it :D But still i really appriciated it thank you very much! I altered my function like the way u did :) – Alexander Mar 14 '11 at 01:42