PHP Regex Multiple Links Paragraph Display Domain Only -
the following code changes every link within paragraph clickable [external link] instead.
i have run twice catch both http , https.
1) there way turn single line?
2) how change instead of [external link] display domain name only? google.com actual link may longer. reason use [external link] shorten longer urls.
//regular http links $text = preg_replace('/(^|[^"])(((f|ht){1}tp:\/\/)[-a-za-z0-9@:%_\+.~#?&\/\/=]+)/i', '\\1<a href="\\2" target="_blank">[external link]</a>', $text); //https links $text = preg_replace('/(^|[^"])(((f|ht){1}tps:\/\/)[-a-za-z0-9@:%_\+.~#?&\/\/=]+)/i', '\\1<a href="\\2" target="_blank">[external link]</a>', $text);
you can add ? make "s" optional.
https://regex101.com/r/lorv7s/1/
$text = preg_replace('/(^|[^"])(((f|ht){1}tps?:\/\/)[-a-za-z0-9@:%_\+.~#?&\/\/=]+)/i', '\\1<a href="\\2" target="_blank">[external link]</a>', $text);
Comments
Post a Comment