Categories

Bubblesort routine

A simple bubblesort example

 

function bubbleSort ($items) {
$size = count($items);
for ($i=0; $i<$size; $i++) {
for ($j=0; $j<$size-1-$i; $j++) {
if ($items[$j+1] < $items[$j]) {
arraySwap($items, $j, $j+1);
}
}
}
return $items;
}
function arraySwap (&$arr, $index1, $index2) {
list($arr[$index1], $arr[$index2]) = array($arr[$index2], $arr[$index1]);
}

Binary Search example

// $low and $high have to be integers

function BinarySearch( $array, $key, $low, $high )
{
if( $low > $high ) // termination case
{
return -1;
}

$middle = intval( ( $low+$high )/2 ); // gets the middle of the array

if ( $array[$middle] == $key ) // if the middle is our key
{
return $middle;
}
elseif ( $key < $array[$middle] ) // our key might be in the left sub-array
{
return BinarySearch( $array, $key, $low, $middle-1 );
}

return BinarySearch( $array, $key, $middle+1, $high ); // our key might be in the right sub-array
}

Parse xml string

<?php
//this is a sample xml string
$xml_string="<?xml version='1.0'?>
<userdb>
    <user name='Jim'>
        <age>35</age>
        <status>admin</status>
    </user>
    <user name='Rob'>
        <age>22</age>
        <status>editor</status>
    </user>
</userdb>";
 
//load the xml string using simplexml function
$xml = simplexml_load_string($xml_string);
 
//loop through the each node of user
foreach ($xml->user as $record)
{
   echo $record['name'], '  ';
   echo $record->age, '  ';
   echo $record->status, '<br />';
}
?>

Random password generator

<?php

function createRandomPassword() 
{
    $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
	//$i is the length of the password
    while ($i < 10) 
	{
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}
 
//Usage example
$password = createRandomPassword();
echo "Your random password is: $password";
 
?>

Social popularity

This example will show how many times your site is mentioned on sites like facebook, twitter and linkedin. The example uses google.

<?php
$url = "http://www.google.com";
$url = rawurlencode($url);
$json_string = file_get_contents_curl('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
$json = json_decode($json_string, true);
echo isset($json['count'])?intval($json['count']):0;

$json_string = file_get_contents_curl('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls='.$url);
$json = json_decode($json_string, true);
echo $json[0]['total_count'] . "facebook likes+shares+comments";

$json_string = file_get_contents_curl("http://www.linkedin.com/countserv/count/share?url=$url&format=json");
$json = json_decode($json_string, true);
echo $json['count'] . " linkedin shares";

$json_string = file_get_contents_curl('http://www.stumbleupon.com/services/1.01/badge.getinfo?url='.$url);
$json = json_decode($json_string, true);
echo $json['result']['views'] . " Stumbleupon views";

$return_data = file_get_contents_curl('http://api.pinterest.com/v1/urls/count.json?url='.$url);
$json_string = preg_replace('/^receiveCount\((.*)\)$/', "\\1", $return_data);
$json = json_decode($json_string, true);
echo $json['count'] . " pins";

function file_get_contents_curl($url)
{
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$cont = curl_exec($ch);
if(curl_error($ch))
{
die(curl_error($ch));
}
return $cont;
}
?>