Archive for the ‘PHP’ Category.

Split a name into First name and Last name using PHP

/** split a name into the first name and last name
 * @param input The name eg 'John Doe'
 * @return Array containing firstname and lastname eg array('firstname'=>'John', 'lastname'=>'Doe');
 */
function splitname($input)
{
   $output = array("firstname"=>"", "lastname"=>"");
   $space = strpos($input, " ");
   if ($space !== false)
   {
      $output['firstname'] = substr($input, 0, $space);
      $output['lastname'] = substr($input, $space, strlen($input));     
   }
   else
   {
      $output['lastname'] = $input;
   } 
   return $output;
}

Convert a url data in to Name Value Pairs array PHP

Convert a url data in to Name Value Pairs array.

/** Convert a url data in to Name Value Pairs array
 * @param url The url and the data string
 * @return array of named value pairs
 */
function urlToNvp($url)
{
   $output = array();
   
   $questionmark = strpos($url, '?');
   if ($questionmark !== false)
   {
      $url = substr($url, $questionmark+1, strlen($url));
   }   
   
   foreach(explode('&', $url) as $data)
   {
      $value = explode('=', $data);
      $output[$value[0]] = $value[1];
   }
      
   return $output;
}

Here are three examples of using this function

print_r(urlToNvp("www.example.co.uk?p1=one&p2=two"));
print_r(urlToNvp("p1=one&p2=two"));
print_r(urlToNvp("www.example.co.uk?p1=one"));

The result

Array ( [p1] => one [p2] => two )
Array ( [p1] => one [p2] => two )
Array ( [p1] => one )

Sound Buttons

Sound Buttons are a technique used to teach children to read. This function generates the sound buttons for a given word, uing the Jolly Phonics system. This approach breaks each word into the groups of letter sounds, of which there are 42 letter sounds.

/**
 * Generates phonics for a given word.
 * This function uses the Jolly Phonics, synthetic phonics programme.    
 * @param input The input word
 * @return array of phonemes
 */   
function phonomes($input)
{
  // create the output array
  $output = array();
  
  // create the Phoneme arrays
  $phoneme3 = array('ear','igh');
  $phoneme2 = array('ai','ar','ch','ck','ee','er','ie','ng','oa','oi','oo','or','ou','ow','qu','sh','th','ue','ue','ur');
  
  // process the input word
  $word =strtolower($input);
  $word = trim($word);
  $letterCount = strlen($word);
  $index = 0;
  
  // iterate each letter in the word searching for phonemes
  while($index <$letterCount)
  {
    if (in_array(substr($word, $index, 3), $phoneme3))
    {
      // found a three letter phoneme
      $output[] = substr($word,$index, 3);
      $index += 3;
    }
    else if (in_array(substr($word, $index, 2), $phoneme2))
    {
      // found a two letter phoneme
      $output[] = substr($word,$index, 2);
      $index += 2;
    }
    else
    {
      //found a single letter phoneme
      $output[] = substr($word,$index, 1);
      $index++;    
    }   
  }
  return $output;
}

To test the phonomes function use

foreach(array('this','moth','that','three','them','thin','quick','quilt','liquid','squid') as $test)
{
  print "$test = " . implode(' - ', phonomes($test)) . "
"; }

This produces the following output

this = th - i - s
moth = m - o - th
that = th - a - t
three = th - r - ee
them = th - e - m
thin = th - i - n
quick = qu - i - ck
quilt = qu - i - l - t
liquid = l - i - qu - i - d
squid = s - qu - i - d

To see this function working please visit Dylan Seaford

Convert a date and time to UTC

/** Converts a date and time to UTC
* /param date The date in the format dd/mm/yyyy
* /param time The time in the format HH/mm/ss
* /return utc
*/   

function utc($inputDate, $inputTime)
{
$pieces = explode("/", $inputDate);
$dateus = $pieces[2]."-".$pieces[1]."-".$pieces[0];
$stringtime = "$dateus $inputTime";
return strtotime($stringtime);

}