Code Snippets

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

  1. /**
  2. * @name         addLinks
  3. * @desc  automatically creates links out of strings that start with 'www' or 'http'
  4. * @author       Brian Huisman - http://www.greywyvern.com
  5. * @since        Version 1.0.0
  6. * @param        string         $string         string of text
  7. * @return       string        the same input string is redisplayed with the links added
  8. */
  9. function addLinks($string) {
  10.   $string = preg_replace("/(?<!quot;|[=\']|:\/\/)\b((\w+:\/\/|www\.).+?)(?=\W*([<>\s]|$))/i", "<a href=\"$1\">$1</a>", $string);
  11.   return preg_replace("/href=\"www/i", "href=\"http://www", $string);
  12. }

RandomString

Generates a random alphanumeric string of whatever length you specify.

  1. /**
  2. * @name   RandomString
  3. * @desc                 function to generate random strings
  4. * @author             Karl Groves
  5. * @copyright    &copy; 2006, Karl Groves
  6. * @param               int    $length    number of characters in the generated string
  7. * @return             string        a new string is created with random characters of the desired length
  8. */
  9. function RandomString($length=32){
  10.         $randstr='';
  11.         srand((double)microtime()*1000000);
  12.         //our array add all letters and numbers if you wish
  13.         $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');
  14.         for ($rand = 0; $rand <= $length; $rand++){
  15.                 $random = rand(0, count($chars) -1);
  16.                 $randstr .= $chars[$random];
  17.         }
  18.         return $randstr;
  19. }

<< Back to category list

Blog

Does Anyone Know Where I Can Get A Tin-Foil Hat

In

[Read more]


News

Announcing Grayscale 2.3

I am proud to announce the release of Grays

[Read more]


Portfolio

View samples of sites built using Grayscale CMS.

[Read more]



Garden Babble