Last active
April 12, 2022 19:23
-
-
Save RundesBalli/3c49de99e16c776c9cf733ffb9f2ebf9 to your computer and use it in GitHub Desktop.
clickableLinks
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 | |
/** | |
* clickableLinks | |
* | |
* Function to automatically link URLs in texts. | |
* | |
* @author RundesBalli <github@rundesballi.com> | |
* @copyright 2022 RundesBalli | |
* @version 1.0 | |
* @license MIT-License | |
* @see https://gist.github.com/RundesBalli/3c49de99e16c776c9cf733ffb9f2ebf9 | |
* | |
* @param string $string String whose URLs are to be linked. | |
* @param bool $newTab True if URLs should open in a new tab. | |
* @param bool $stripTags True if HTML and PHP tags should be removed before convertion. | |
* | |
* @return string The string with linked URLs. | |
*/ | |
function clickableLinks(string $string, bool $newTab = TRUE, bool $stripTags = TRUE) { | |
return preg_replace( | |
'/https?:\/\/[^\s]+/im', | |
'<a href=\'$0\''.($newTab ? ' target=\'_blank\' rel=\'noopener\'' : NULL).'>$0</a>', | |
($stripTags ? strip_tags($string) : $string) | |
); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment