Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created July 22, 2020 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpfiddle/9a22680579edfa2aff50cffc8cac1abd to your computer and use it in GitHub Desktop.
Save phpfiddle/9a22680579edfa2aff50cffc8cac1abd to your computer and use it in GitHub Desktop.
[ Posted by ARUN AK ] Links update in a give text.
<?php
$string = "Hallo Studenten http://google.com/your/subpage and https://www.yahoo.com/my/subpage";
$regexp = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i";
$anchorMarkup = "<a href=\"%s://%s\" target=\"_blank\" >%s</a>";
preg_match_all($regexp, $string, $matches, \PREG_SET_ORDER);
echo '<pre>';print_r($matches);echo '</pre>';
foreach ($matches as $match) {
if (empty($match[1]) && empty($match[7])) {
$http = $match[2]?$match[2]:'http';
$replace = sprintf($anchorMarkup, $http, $match[0], $match[0], $match[1]);
$string = str_replace($match[0], $replace, $string);
}
}
echo $string;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment