PHP Classes

File: tinyMVC/lib/tinyMVC.php

Recommend this page to a friend!
  Classes of Alan H. Lake   tinyMVC   tinyMVC/lib/tinyMVC.php   Download  
File: tinyMVC/lib/tinyMVC.php
Role: Class source
Content type: text/plain
Description: utility functions for use with tinyMVC
Class: tinyMVC
MVC based Web application framework
Author: By
Last change: Added path to File name. Added code to contents.
Date: 13 years ago
Size: 2,355 bytes
 

Contents

Class file image Download
// Miscellaneous functions for use in an application.
<?php
class tinyMVC
{
    function
array_to_string($params, $separators=array(': ',"\n"))
    {
        if(
is_array($params)) {
           
$str = '';
            foreach(
$params as $key => $value) {
                if(
strlen($str) > 0) {
                   
$str .= $separators[1];
                }
               
$str .= $key.$separators[0].$value;
            }
            return
$str;
        }elseif(
is_string($params)) {
            return
$params;
        }
        return
false;
    }

    function
google_search(
     
$q = NULL, $start = 1, $num = 19, $lr = "", $hl = "en", $which_google = 'com')
    {
       
$q = rawurlencode($q);
       
$start = rawurlencode($start);
       
$num = rawurlencode($num);
       
$lr = rawurlencode($lr);
       
$hl = rawurlencode($hl);
       
$url = "http://www.google.$which_google/search?q=$q&start=$start&num=$num";
        if(
$lr != '') {
           
$url .= "&lr=$lr";
        }
       
$url .= "&hl=$hl&btnG=Google+Search&aq=f&oq=";
        while(
true) {
            try {
                return
file_get_contents($url);
            } catch (
Exception $e) {
                echo
"Failed Google search with \"$url\"<br />\n";
            }
        }
    }

    function
humanize($str,$dehumanize=false)
    {
        if(
$dehumanize) {
            return
strtolower(str_replace(' ','_',$str));
        }
        return
ucwords(str_replace('_',' ',$str));
    }

    function
seconds_to_hms($secs)
    {
       
$seconds = $secs % 60;
       
$secs = $secs - $seconds;
       
$minutes = ($secs / 60) % 60;
       
$secs = $secs - ($minutes * 60);
       
$hours = $secs / 3600;
        return
$hours.':'.$minutes.':'.$seconds;
    }

    function
strip_tags($str)
    {
        do {
           
$count = 0;
           
$str = preg_replace('/(<)([^>]*?<)/' , '&lt;$2' , $str , -1 , $count);
        } while (
$count > 0);
       
$str = strip_tags($str);
       
$str = str_replace('>' , '&gt;' , $str);
        return
$str;
    }

    function
zeropad($number, $length)
    {
    
/*
      * Add zeros to the begining of the string until it reaches desired length
      * Example:
      * $number_helper->zeropad(123, 6) => 000123
      */
       
return str_pad($number, $length*-1, '0');
    }

}
?>