1

How do I go about getting link text (like markdown). Say some one types

(Google)[https://google.com]

The result would look like:

<a href="https://google.com">Google</a>

Here's my current function:

function MakeUrls($str)
{
    $find=array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`si','`((?<!//)(www\.\S+[[:alnum:]]/?))`si');

    $replace=array('<a href="$1" target="_blank"> $0</a>', '<a href="http://$1" target="_blank">$1</a>');

    return preg_replace($find,$replace,$str);
}

When someone types https://google.com it returns:

<a href="https://google.com" target="_blank"> https://google.com</a>
Kevin S
  • 173
  • 2
  • 10
  • If you're happy to use javascript there's this: [Existing question](https://stackoverflow.com/questions/7901760/how-can-i-get-the-title-of-a-webpage-given-the-url-an-external-url-using-jquer) – Moustachio Jun 12 '17 at 23:54
  • That's not it man. – Kevin S Jun 13 '17 at 00:21

1 Answers1

0
function makeURLs($str) {
  $explode = explode(")[", $str);
  if (sizeof($explode) !== 2) return false; //failsafe stuff

  $name = substr($explode[0], 1, strlen($explode[0]));  //remove the (
  $url = substr($explode[1], 0, strlen($explode[1])-1); //remove the ]

  return "<a href=\"$url\" target=\"_blank\">$name</a>";
}

should do it