5

I have a slight issue that I am in need of help with.

Say if $post['text'] was equal to data like: "https://stackoverflow.com/blah?blah is such a cool website", how would it be possible to make just the URL bit in a type tag...

So the end HTML result would be somthing like

<p><a href="https://stackoverflow.com/blah?blah">https://stackoverflow.com/blah?blah</a> is such a cool website</p>

I am a bit of a noob. I'm actually a 14 year old kid and still kinda new to code, but I loveee it!

Community
  • 1
  • 1
CALI1198
  • 51
  • 4
  • You want to take arbitrary text, find URLs in it, and turns those into HTML links? – Brad Jul 13 '13 at 18:02
  • See http://htmlparsing.com/php for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged. – Andy Lester Jul 13 '13 at 18:03
  • 5
    + for a such a young, eager member joining StackOverflow. Welcome! – Jason McCreary Jul 13 '13 at 18:21
  • Awesome! Thanks for the help people. Was able to use the function from http://stackoverflow.com/questions/1925455/how-to-mimic-stackoverflow-auto-link-behavior . Then around my $post['text'], just add auto_link_text($post['text']) !. Cheers – CALI1198 Jul 13 '13 at 18:24
  • 1
    Keep it up @user2579676 - and take note of how politely Brad helped you find the answer in an existing question/answer. Do your best to find the answer before you ask, but that might not always work until you learn how to reframe the question, which comes from experience. Ask when you need to and let the old dogs help you find the right answers. – bsoist Jul 13 '13 at 19:07

2 Answers2

3

You can use regular expersion.

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

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

    // return $text
    return $text;
}

echo make_hyperlink('http://stackoverflow.com/blah?blah');

Use above function. It will replace URL from a simple url text.

Raju Singh
  • 750
  • 8
  • 21
1
   <p> <a href="<?=$_POST['text']?>"> <?=$_POST['text']?> </a> is a cool website </p>
  • Yes but the var has other text within it. It's not JUST the link. But I have found a workaround already. Thank you for your response though! – CALI1198 Jul 15 '13 at 18:01