Last active
May 4, 2016 09:31
-
-
Save Anan5a/2b223dea5886d6b70b5cac6d87587eee to your computer and use it in GitHub Desktop.
A short snippet to convert a linkable text from a text to an anchor link
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 | |
/** | |
* @File auto_link.php | |
* @Author Ananta | |
**/ | |
function auto_link($data_src){ | |
/** | |
*Check if the url is an insecure http:// url | |
or short // url | |
* | |
**/ | |
if(preg_match('/http:\/\//',$data_src) || preg_match('/\/\//',$data_src) ){ | |
/** | |
*Convert for http:// | |
**/ | |
$http= preg_replace("/http:\/\/(.*){}/","<a href=\"\${0}\">\${0}</a>",$data_src); | |
return preg_replace('/{}/','',$http); | |
} | |
/** | |
*else it'll be https:// | |
**/ | |
$https= preg_replace("/https:\/\/(.*){}/","<a href=\"\${0}\">\${0}</a>",$data_src); | |
return preg_replace('/{}/','',$https); | |
} | |
/** | |
*Note: It'll only convert http:// or https:// or // protocols | |
*otherwise it'll be plain text | |
**/ | |
/** | |
*Usage | |
auto_link($source); | |
**/ | |
// | |
// | |
// | |
// | |
/** | |
*It require a special link ending identifier {} after the link. | |
*like http://mylink.com/{} or | |
*http://mylink.com{} | |
*work for //mysite.com{} | |
*other wise it wouldn't work properly | |
**/ | |
/** | |
*example | |
**/ | |
$source='Hello. I\'m from http://weblolli.blogspot.com{}'; | |
$source2='Hello. I\'m from https://weblolli.blogspot.com/{}'; | |
$source3='Hello. I\'m from //weblolli.blogspot.com{}'; | |
echo auto_link($source).'<br>'; | |
echo auto_link($source2).'<br>'; | |
echo auto_link($source3); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment