Code Snippets
Category: Custom Functions
Helpful functions you can use in your PHP applications.
addLinks
This function automatically creates links from text that is formed like an URL
- /**
- * @name addLinks
- * @desc automatically creates links out of strings that start with 'www' or 'http'
- * @author Brian Huisman - http://www.greywyvern.com
- * @since Version 1.0.0
- * @param string $string string of text
- * @return string the same input string is redisplayed with the links added
- */
- function addLinks($string) {
- $string = preg_replace("/(?<!quot;|[=\']|:\/\/)\b((\w+:\/\/|www\.).+?)(?=\W*([<>\s]|$))/i", "<a href=\"$1\">$1</a>", $string);
- return preg_replace("/href=\"www/i", "href=\"http://www", $string);
- }
RandomString
Generates a random alphanumeric string of whatever length you specify.
- /**
- * @name RandomString
- * @desc function to generate random strings
- * @author Karl Groves
- * @copyright © 2006, Karl Groves
- * @param int $length number of characters in the generated string
- * @return string a new string is created with random characters of the desired length
- */
- function RandomString($length=32){
- $randstr='';
- srand((double)microtime()*1000000);
- //our array add all letters and numbers if you wish
- $chars = array ( 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9');
- for ($rand = 0; $rand <= $length; $rand++){
- $random = rand(0, count($chars) -1);
- $randstr .= $chars[$random];
- }
- return $randstr;
- }




