Created
July 22, 2020 13:02
-
-
Save phpfiddle/9a22680579edfa2aff50cffc8cac1abd to your computer and use it in GitHub Desktop.
[ Posted by ARUN AK ] Links update in a give text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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