function soundex ($word
{
$letters = 'abcdefgijklmnopqrstuvxyz'
$codes = '012301202245501262301202'
$word = strtolower$word);
$soundex = ''
$count = 0;
for ($ii=0; $iistrlen$word); $ii++) {
$letter = $word$ii];
$p = strpos$letters, $letter);
if ($p!==false) {
if ($codes$p]!==substr$soundex,-1)) {
$soundex .= $codes$p];
if ($codes$p]!='0') $count++; //count only consonants
if ($count>=5) break; //if we have 5 characters we're sure we can break
}
}
}
// add initial letter
$init = substr$word, 0, 1);
$p = strpos$letters, $init);
if ($p===false)
$soundex = strtoupper$init).$soundex
else
$soundex = strtoupper$init).substr$soundex, 1);
// get rid of the vowels
$soundex = str_replace'0', '', $soundex);
// trim or pad
if (strlen$soundex)>=4) {
$soundex = substr$soundex, 0, 4);
} else {
$soundex .= str_repeat'0', 4-strlen$soundex));
}
return $soundex
}
Category: View
eXorithm – Execute Algorithm: View / Run Algorithm php_manual
function php_manual ($function_name
{
$function_name = strtolowerstr_replace'_', '-', $function_name));
return "http://www.php.net/manual/en/function."$function_name".php"
}
eXorithm – Execute Algorithm: View / Run Algorithm isbn_hyphenate
function isbn_hyphenate ($isbn
{
$isbnpreg_replace'/[^dX]/'''$isbn); //remove all non-numeric chars
// strip prefix from ISBN13
if (strlen$isbn)==13) {
$prefix = substr$isbn,0,3).'-'
$isbn = substr$isbn,3);
} else if (strlen$isbn)==10) {
$prefix = ''
} else {
return ''
}
$unknown = substr$isbn,1,4)+0;
$publisher = ''
$unit = ''
if (($isbn[0] == '0') || ($isbn[0] == '3') || ($isbn[0] == '4'))
// english region 0
// german region 3
// japan region 4
{
if$unknown<=1999)
{
$publisher= substr$isbn,1,2);
$unit= substr$isbn,3,6);
}
elseif$unknown>=2000 && $unknown<=6999)
{
$publisher= substr$isbn,1,3);
$unit= substr$isbn,4,5);
}
elseif$unknown>=7000 && $unknown<=8499)
{
$publisher= substr$isbn,1,4);
$unit= substr$isbn,5,4);
}
elseif$unknown>=8500 && $unknown<=8999)
{
$publisher= substr$isbn,1,5);
$unit= substr$isbn,6,3);
}
elseif$unknown>=9000 && $unknown<=9499)
{
$publisher= substr$isbn,1,6);
$unit= substr$isbn,7,2);
}
elseif$unknown>=9500)
{
$publisher= substr$isbn,1,7);
$unit= $isbn[8];
}
return $prefix$isbn[0]."-"$publisher"-"$unit"-"$isbn[9];
}
else if ($isbn[0] == '1'
// english region 1
{
if$unknown<=999)
{
$publisher= substr$isbn,1,2);
$unit= substr$isbn,3,6);
}
elseif$unknown>=1000 && $unknown<=3999)
{
$publisher= substr$isbn,1,3);
$unit= substr$isbn,4,5);
}
elseif$unknown>=4000 && $unknown<=5499)
{
$publisher= substr$isbn,1,4);
$unit= substr$isbn,5,4);
}
elseif$unknown>=5500 && $unknown<=8697)
{
$publisher= substr$isbn,1,5);
$unit= substr$isbn,6,3);
}
elseif$unknown>=8698 && $unknown<=9989)
{
$publisher= substr$isbn,1,6);
$unit= substr$isbn,7,2);
}
elseif$unknown>=9990)
{
$publisher= substr$isbn,1,7);
$unit= $isbn[8];
}
return $prefix$isbn[0]."-"$publisher"-"$unit"-"$isbn[9];
}
else
// other regions are not fully supported
{
return $prefixsubstr$isbn,0,9)."-"$isbn[9];
}
}
eXorithm – Execute Algorithm: View / Run Algorithm conv_isbn
function conv_isbn ($isbn
{
$isbn = "978" . $isbn; //add ‘978’ prefix
$isbn = str_split$isbn); //split into 13-element array
$m=3; //initialize variables
$sum=0;
for$i=0; $i<12; $i++)
{
($m==1) ? $m=3 : $m=1; //alternate $m between 1 & 3
$sum+=($isbn$i]*$m); //sum products
}
$isbn[12]=10-($sum%10); //Write new check digit (10 - $sum%10)
$isbn = implode$isbn);
return $isbn
}
eXorithm – Execute Algorithm: View / Run Algorithm word_counts
function word_counts ($text, $noise
{
$words = preg_split'/[^A-Za-z]+/', strtolower$text));
$counts = array();
foreach ($words as $word) {
if (strlen$word)>1) { // 1-letter words are ignored
if (array_search$word, $noise)===false) { // noise word?
if (array_key_exists$word, $counts)) {
$counts$word] = $counts$word]+1;
} else {
$counts$word] = 1;
}
}
}
}
return $counts
}
eXorithm – Execute Algorithm: View / Run Algorithm make_change
function make_change ($amount, $coins
{
$coin_count = count$coins);
$table = array();
for ($i = -1; $i <= $amount; $i++) {
for$j = -1; $j <= $coin_count; $j++) {
// Rules
// 1: table[0,0] or table[0,x] = 1
// 2: talbe[i <= -1, x] = 0
// 3: table[x, j <= -1] = 0
$total = 0;
// first sub-problem
// count(n, m-1)
$n = $i
$m = $j-1;
if ($n == 0) // rule 1
$total += 1;
else if ($n <= -1) // rule 2
$total += 0;
else if (($m <= 0) && ($n >= 1))
$total += 0;
else
$total += $table$n][$m];
// second sub-problem
// count(n-S[m], m)
if (($j-1) <= -1)
$total += 0;
else {
$n = $i - $coins$j - 1];
$m = $j
if ($n == 0) // rule 1
$total += 1;
else if ($n <= -1) // rule 2
$total += 0;
else if (($m <= 0) && ($n >= 1)) // rule 3
$total += 0;
else
$total += $table$n][$m];
}
$table$i][$j] = $total
}
}
return $table$i-1][$j-1];
}
eXorithm – Execute Algorithm: View / Run Algorithm array_rsearch
function array_rsearch ($search, $array, $strict
{
$array = array_reverse$array, true);
foreach ($array as $key => $value){
if ($strict) {
if ($value === $search
return $key
} else {
if ($value == $search
return $key
}
}
return false;
}
eXorithm – Execute Algorithm: View / Run Algorithm create_gradient
function create_gradient ($start_color, $end_color, $size, $thickness, $orientation
{
if ($orientation=="vertical") {
$imgimagecreatetruecolor$thickness$size);
} else {
$imgimagecreatetruecolor$size$thickness);
}
$start_r = hexdecsubstr$start_color, 0, 2));
$start_g = hexdecsubstr$start_color, 2, 2));
$start_b = hexdecsubstr$start_color, 4, 2));
$end_r = hexdecsubstr$end_color, 0, 2));
$end_g = hexdecsubstr$end_color, 2, 2));
$end_b = hexdecsubstr$end_color, 4, 2));
for ($i=0;$i$size$i++) {
$red = round$start_r - ($start_r$end_r) * ($i / ($size-1)));
$green = round$start_g - ($start_g$end_g) * ($i / ($size-1)));
$blue = round$start_b - ($start_b$end_b) * ($i / ($size-1)));
$color = imagecolorallocate$img, $red, $green, $blue);
if ($orientation=="vertical") {
for ($k=0;$k$thickness$k++)
imagesetpixel$img, $k, $i, $color);
} else {
for ($k=0;$k$thickness$k++)
imagesetpixel$img, $i, $k, $color);
}
}
return $img
}
eXorithm – Execute Algorithm: View / Run Algorithm insertion_sort
function insertion_sort ($array
{
for$ii=1; $iicount$array); $ii++) {
$value = $array$ii];
$done = false;
$jj = $ii-1;
do {
if ($array$jj] > $value) {
$array$jj+1] = $array$jj];
$jj = $jj-1;
if ($jj<0)
$done = true;
} else {
$done = true;
}
} while (!$done);
$array$jj+1] = $value;
}
return $array
}
eXorithm – Execute Algorithm: View / Run Algorithm calculate_median
function calculate_median ($numbers
{
sort$numbers);
$c = count$numbers) - 1;
if ($c%2) {
$b = round$c/2);
$a = $b-1;
return ($numbers$b] + $numbers$a]) / 2 ;
} else {
return $numbers[($c/2)];
}
}