Validate Domain

<?php

/**
 * validate_domain
 *
 * validate domain
 *
 * @version 0.5
 * @author Contributors at eXorithm
 * @link /algorithm/view/validate_domain Listing at eXorithm
 * @link /algorithm/history/validate_domain History at eXorithm
 * @license /home/show/license
 *
 * @param mixed $domain 
 * @return bool
 */
function validate_domain($domain='www.google.com')
{
	return preg_match ("/^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu)$/i", $domain);
}

?>

Draw UPC Barcode

<?php

/**
 * draw_upc_barcode
 *
 * Draw a barcode for a UPC number.
 *
 * @version 0.3
 * @author Contributors at eXorithm
 * @link /algorithm/view/draw_upc_barcode Listing at eXorithm
 * @link /algorithm/history/draw_upc_barcode History at eXorithm
 * @license /home/show/license
 *
 * @param mixed $number The 12-digit UPC number.
 * @param bool $show_numbers Whether to draw the numbers at the bottom of the barcode.
 * @return resource GD image
 */
function draw_upc_barcode($number='925853043217',$show_numbers=true)
{
	$number = str_replace(array('-',' '), '', $number);
	
	if (strlen($number)!=12)
		throw new Exception("UPC number must have 12 digits");
	
	for ($i=0;$i<12;$i++) {
		if (!is_numeric($number[$i]))
			throw new Exception("UPC number must contain only digits");
	}
	
	$lcodes = array(
		'0001101',
		'0011001',
		'0010011',
		'0111101',
		'0100011',
		'0110001',
		'0101111',
		'0111011',
		'0110111',
		'0001011',
	);
	
	$rcodes = array(
		'1110010',
		'1100110',
		'1101100',
		'1000010',
		'1011100',
		'1001110',
		'1010000',
		'1000100',
		'1001000',
		'1110100',
	);
	
	$code = '101';
	for ($i=0;$i<6;$i++) {
		$code .= $lcodes[$number[$i]];
	}
	$code .= '01010';
	for ($i=6;$i<12;$i++) {
		$code .= $rcodes[$number[$i]];
	}
	$code .= '101';
	
	// create image
	$width=190;
	$height=100;
	$image = image_create_alpha($width, $height);
	
	$white = imagecolorallocate($image, 255, 255, 255);
	imagefilledrectangle($image, 0, 0, $width, $height, $white);
	
	// draw lines
	$black = imagecolorallocate($image, 0, 0, 0);
	for ($i=0;$i<strlen($code);$i++) {
		if ($code[$i]=='1') {
			imageline($image, $i*2,0, $i*2, $height, $black);
			imageline($image, $i*2+1,0, $i*2+1, $height, $black);
		}
	}
	
	// draw numbers
	if ($show_numbers) {
		imagefilledrectangle($image, 6, $height-16, 90, $height, $white);
		imagefilledrectangle($image, 98, $height-16, 182, $height, $white);
		for ($i=0;$i<6;$i++) {
			imagestring($image, 2, 11+$i*14, $height-14, $number[$i], $black);
		}
		for ($i=6;$i<12;$i++) {
			imagestring($image, 2, 19+$i*14, $height-14, $number[$i], $black);
		}
	}
	
	return $image;
}

/**
 * image_create_alpha
 *
 * Helper function to create a new blank image with transparency.
 *
 * @version 0.1
 * @author Contributors at eXorithm
 * @link /algorithm/view/image_create_alpha Listing at eXorithm
 * @link /algorithm/history/image_create_alpha History at eXorithm
 * @license /home/show/license
 *
 * @param mixed $width 
 * @param mixed $height 
 * @return resource GD image
 */
function image_create_alpha($width='',$height='')
{
	// Create a normal image and apply required settings
	$img = imagecreatetruecolor($width, $height);
	imagealphablending($img, false);
	imagesavealpha($img, true);
	
	// Apply the transparent background
	$trans = imagecolorallocatealpha($img, 0, 0, 0, 127);
	for ($x = 0; $x < $width; $x++)
	{
		for ($y = 0; $y < $height; $y++)
		{
			imagesetpixel($img, $x, $y, $trans);
		}
	}
	
	return $img;
}

?>

Unity

<?php

/**
 * unity
 *
 * Returns digit summation of any numerical value given for passkey (up to 14 digits in length)
 *
 * @version 1.2
 * @author Contributors at eXorithm
 * @link /algorithm/view/unity Listing at eXorithm
 * @link /algorithm/history/unity History at eXorithm
 * @license /home/show/license
 *
 * @param number $passkey enter any numerical "whole" digits (non-negative)
 * @return mixed
 */
function unity($passkey=75025)
{
	$ubn = array();
	
	while ($passkey > 9) {
		if (strlen($passkey) > 1)
			$passkey = array_sum(str_split($passkey));
		else
			$passkey = $passkey;
	} 
	
	$ubn[] = $passkey;
	
	return end($ubn);
}

?>

Unique Image

<?php

/**
 * unique_image
 *
 * Generate a pseudo-unique "hash" image based on a string.
 *
 * @version 0.3
 * @author Contributors at eXorithm
 * @link /algorithm/view/unique_image Listing at eXorithm
 * @link /algorithm/history/unique_image History at eXorithm
 * @license /home/show/license
 *
 * @param mixed $string 
 * @return resource GD image
 */
function unique_image($string='whatever')
{
	$size=200;
	$steps=5;
	$step=$size/$steps;
	
	$image = image_create_alpha($size, $size);
	
	$n = 0;
	$prev = 0;
	$len = strlen($string);
	$sum = 0;
	for ($i=0;$i<$len;$i++) $sum += ord($string[$i]);
	
	for ($i=0;$i<$steps;$i++) {
		for ($j=0;$j<$steps;$j++) {
			$letter = $string[$n++ % $len];
			
			$u = ($n % (ord($letter)+$sum)) + ($prev % (ord($letter)+$len)) + (($sum-1) % ord($letter));
			$color = imagecolorallocate($image, pow($u*$prev+$u+$prev+5,2)%256, pow($u*$prev+$u+$prev+3,2)%256, pow($u*$prev+$u+$prev+1,2)%256);
			if (($u%2)==0)
				imagefilledpolygon($image, array($i*$step, $j*$step, $i*$step+$step, $j*$step, $i*$step, $j*$step+$step), 3, $color);
			$prev = $u;
			
			$u = ($n % (ord($letter)+$len)) + ($prev % (ord($letter)+$sum)) + (($sum-1) % ord($letter));
			if (($u%2)==0)
				imagefilledpolygon($image, array($i*$step, $j*$step+$step, $i*$step+$step, $j*$step+$step, $i*$step+$step, $j*$step), 3, $color);
			$prev = $u;
		
		}
	}
	
	return $image;
}

/**
 * image_create_alpha
 *
 * Helper function to create a new blank image with transparency.
 *
 * @version 0.1
 * @author Contributors at eXorithm
 * @link /algorithm/view/image_create_alpha Listing at eXorithm
 * @link /algorithm/history/image_create_alpha History at eXorithm
 * @license /home/show/license
 *
 * @param mixed $width 
 * @param mixed $height 
 * @return resource GD image
 */
function image_create_alpha($width='',$height='')
{
	// Create a normal image and apply required settings
	$img = imagecreatetruecolor($width, $height);
	imagealphablending($img, false);
	imagesavealpha($img, true);
	
	// Apply the transparent background
	$trans = imagecolorallocatealpha($img, 0, 0, 0, 127);
	for ($x = 0; $x < $width; $x++)
	{
		for ($y = 0; $y < $height; $y++)
		{
			imagesetpixel($img, $x, $y, $trans);
		}
	}
	
	return $img;
}

?>

Simple Sort

<?php

/**
 * simple_sort
 *
 * Sort an array and display it.
 *
 * @version 0.1
 * @author Contributors at eXorithm
 * @link /algorithm/view/simple_sort Listing at eXorithm
 * @link /algorithm/history/simple_sort History at eXorithm
 * @license /home/show/license
 *
 * @param array $array 
 * @return array
 */
function simple_sort($array=array(0=>'1',1=>'11',2=>'22',3=>'3',4=>'4'))
{
	sort($array);
	return $array;
}

?>

Magic8Ball

<?php

/**
 * magic8ball
 *
 * Ask the magic 8 ball your question.
 *
 * @version 0.2
 * @author Contributors at eXorithm
 * @link /algorithm/view/magic8ball Listing at eXorithm
 * @link /algorithm/history/magic8ball History at eXorithm
 * @license /home/show/license
 *
 * @return mixed
 */
function magic8ball()
{
	$answers =array('It is certain', 'It is decidedly so', 'Without a doubt',
				'Yes โ€“ definitely', 'You may rely on it', 'As I see it, yes',
				'Most likely', 'Outlook good', 'Signs point to yes', 'Yes',
				'Reply hazy, try again', 'Ask again later',
				'Better not tell you now', 'Cannot predict now',
				'Concentrate and ask again', 'Don\'t bet on it',
				'My reply is no', 'My sources say no', 'Outlook not so good',
				'Very doubtful' );
	
	$index = rand(0, count($answers));
	return ($answers[$index]);
}

?>