Created
July 20, 2021 14:42
-
-
Save arjanfrans/5d25027327f62809465cd98677f91e6b to your computer and use it in GitHub Desktop.
urls_to_hyperlinks.php
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
/** | |
* Find all url's and turn them into hyperlinks if they are not already. | |
*/ | |
public function urlsToHyperlinks(string $text): string | |
{ | |
return preg_replace( | |
'~(?<!href=[\'"])https?://[\w/._\-&;=?]*(?!</a>)(?=[^\w/._\-&])~s', | |
'<a href="$0" target="_blank" rel="noopener noreferrer">$0</a>', | |
$text | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment