BigToach.com

PHP Snippets

1 ... « 8 9 10

get_month

Information

(PHP Version >= 4.05)
Functions used in this snippet: in_array, strtolower, array_search, strtotime.

Description

array get_month( str month, mixed year)

Takes a text month and numeric year and returns the unix timestamps for the very beginning of the month, and very end of the month.
Returns false on failure.

Snippet

<?php
function get_month($month,$year){
    
$months = array('january''february''march''april''may''june''july''august''september''october''november''december');
    
$ret = array();
    if(
in_array(strtolower($month), $months)){
        
$ret[] = strtotime($month ' 1, ' $year ' 00:00');
        
$nextmonth $months[array_search($month,$months)];
        
$year $nextmonth === 'january' $year++ : $year
        
$ret[] = strtotime($nextmonth ' 1, ' $year ' 00:00 -1 second');
        return 
$ret;
    }
    return 
false;
}
?>

Example

get_month Can be used in the following way:

<?php
$time 
get_month('january','2007');
echo 
$time[0] . ' ' $time[1]; // outputs "1167638400 1169798399"
$time get_month('july',2000);
echo 
date('r',$time[0]) . ' ' date('r',$time[1]);
// outputs "Sat, 1 Jul 2000 00:00:00 -0700 Tue, 25 Jul 2000 23:59:59 -0700"
?>

Added on Jun 26th at 6 am by Scott - 0 Comments
Updated on Jun 26th at 8 am.


find_version

Information

(PHP Version >= 4.3.0)
Functions used in this snippet: str_replace, explode, preg_match, strstr, substr, strpos, preg_replace, file_get_contents.

Description

str find_version(str functions)

Finds the newest PHP version needed out of a list of functions. A string is returned with the newest PHP version needed.

Snippet

<?php
function find_version($functions) {
        
$str str_replace(array("\r","\n",' ','(',')'),'',$functions);
        
$fun explode(',',$str);
        
$versions = array();
        foreach(
$fun as $key=>$value){
            
$str file_get_contents('http://www.php.net/' str_replace('_','-',$value));
            
preg_match('/\>\s*\((.+?)\)\<\/P/ims'$str,$matches);
            
$matches[0] = str_replace(array('3 >= ','4 >= ','5 >= '),'',$matches[0]);
            
$x preg_replace('/[^,0-9.]/','',$matches[0]);
            
$x strstr($x,',') ? substr($x,0,strpos($x,',')) : $x;
            
$versions[] = $x;
        }
        
natsort($versions);
        
$x end($versions);
        return 
$x;
    }
?>

Example

find_version Can be used in the following way:

<?php
echo find_version('file_get_contents,substr,in_array');
// above outputs 4.3.0

echo find_version('str_replace(),str_ireplace()');
// above outputs 5
?>

Added on Jun 25th at 4 am by Scott - 0 Comments
Updated on Jun 25th at 5 am.


bbasic

Information

(PHP Version >= 3.0.9)
Functions used in this snippet: preg_replace.

Description

str bbasic(str string)

Replaces [b],[i], and [type] with bold text, italicized text, and colored text respectively. The Functions returns an adjusted str of the supplied string.

Snippet

<?php
function bbasic($str){
    
$str preg_replace('/\[b\](.+?)\[\/b\]/ims','<span class="bold">$1</span>',$str);
    
$str preg_replace('/\[i\](.+?)\[\/i\]/ims','<span class="italic">$1</span>',$str);
    
$str preg_replace('/\[type\](.+?)\[\/type\]/ims','<span class="type">$1</span>',$str);
    return 
$str;
}
?>

Example

bbasic Can be used in the following way:

<?php
echo bbasic('[b]yay this is bold[b]');
// above text is turned to <span class="bold">yay this is bold</span>
echo bbasic('[type]string[/type] are [i]italic[/i]');
// above text is turned to <span class="type">string</span> are <span class="italic">italic</span>
?>

Added on Jun 24th at 8 pm by Scott - 0 Comments
Updated on Jun 27th at 3 am.


timer

Information

(PHP Version >= 4.0.4)
Functions used in this snippet: array_sum, microtime, explode, number_format.

Description

float timer([float start,[int num]])

sets a start timer and outputs another timer less start to num decimals.

Snippet

<?php
function timer($start='',$num=8){
    if(
$start === ''){
        return 
array_sum(explode(' ',microtime()));
    }else{
        return 
number_format(timer() - $start,$num);
    }
}
?>

Example

timer Can be used in the following way:

<?php
$start 
timer();
// code to be timed goes here
echo timer($start); // shows elapsed time to 8 decimal places

$start timer();
// code to be timed goes here
echo timer($start,12); // shows elapsed time to 12 decimal places
?>

Added on Jun 23rd at 9 pm by Scott - 0 Comments
Updated on Jul 1st at 4 am.


stri_replace

Information

(PHP Version >= 4.10)
Functions used in this snippet: array_key_exists, count, is_array, array_keys, strtolower, str_replace, explode, strlen, substr.

Description

str stri_replace(mixed search, mixed replace, str subject)

replaces all occurances of search with replace in the subject string and returns the corrected string. It is done without regards to text case.

search and replace can bot be arrays.

Snippet

<?php
function stri_replace($word,$replace,$str){
    
$wordray is_array($word) ? true false;
    
$replray is_array($replace) ? truefalse;
    if(
$wordray){
        
$wordkeys array_keys($word);
        
$sizeword count($wordkeys);
        if(
$replray){
            
$replkeys array_keys($replace);
            
$sizerepl count($replace);
        }
        for(
$i=0$i<$sizeword$i++){
            if(
$replray && array_key_exists($i,$replkeys)){
                
$repll $replace[$replkeys[$i]];
            }elseif(
$replray){
                
$repll '';
            }else{
                
$repll $replace;
            }
            
$str stri_replace($word[$wordkeys[$i]],$repll,$str);
        }
        
$ret $str;
    }else{
        
$str_low strtolower($str);
        
$word_low strtolower($word);
        
$str_low str_replace($word_low,$word,$str_low);
        
$explode explode($word,$str_low);
        
$count count($explode);
        
$ret '';
        
$holder 0;
        
$len strlen($word);
        for(
$i=0$i<$count$i++){
            
$thislen strlen($explode[$i]);
            
$ret .= substr($str,$holder,$thislen);
            if(
$i !== ($count 1)){
                
$ret .= $replace;
            }
            
$holder $holder $thislen $len;
        }
    }
    return 
$ret;
}
?>

Example

stri_replace Can be used in the following way:

<?php
$str 
'I HAte Red and love Blue';
echo 
stri_replace('hate','despise',$str); // I despise Red and love Blue
echo stri_replace(array('hate','LOVE'), array('enjoy','despise'),$str);
// I enjoy Red and despise Blue
?>

Added on Jun 23rd at 9 pm by Scott - 1 Comment
Updated on Jun 25th at 1 am.


1 ... « 8 9 10