BigToach.com

PHP Snippets

find_functions

Information

(PHP Version >= 4)
Functions used in this snippet: preg_replace, preg_match_all, in_array, implode.

Description

str find_functions( str str )

Finds every function in the string str. It returns a string of the functions seperated by commas with no spaces.

Snippet

<?php
function find_functions($str)
{
    
$str preg_replace('/function ([a-z0-9_]+)\s?\(/ims','',$str);
    
preg_match_all('/([a-z0-9_]+)\s?\(/ims',$str,$matches);
    
$reserved = array('if''foreach''for''do''while''array''print''switch''echo');
    
$ret = array();
    foreach(
$matches[1] as $key=>$function)
    {
        if(!
in_array($function$reserved))
        {
            
$ret[] = $function;
            
$reserved[] = $function;
        }
    }
    return 
implode(',',$ret);
}
?>

Example

find_functions Can be used in the following way:

<?php
$str 
'$var = str_replace("haha", "wee", "haha wee");
print_r($_SERVER);
echo substr ($_SERVER["REMOTE_ADDR"], 0, strpos($_SERVER["REMOTE_ADDR"], ".") -1);
echo pi();'
;
echo 
find_functions($str);

// returns the following
// str_replace,print_r,substr,strpos,pi
?>

Added on Oct 23rd at 9 am by Scott - 4 Comments


array_natcase

Information

(PHP Version >= 3)
Functions used in this snippet: natcasesort, array_reverse.

Description

array array_natcase( str key, array array[, str ascdesc] )

Sorts the associative array array by the associative array key key with natcasesort. It maintains all keys and can be sorted ascending or descending by setting ascdesc to either asc, or desc. The function returns the newly array sorted array.

Snippet

<?php
function array_natcase($key$array$ascdesc='asc')
{
    
$temp = array();
    
$final = array();
    foreach(
$array as $id=>$value)
    {
        
$temp[$id] = $value[$key];
    }
    
natcasesort($temp);
    foreach(
$temp as $id=>$value)
    {
        
$final[$id] = $array[$id];
    }
    if(
$ascdesc{0} === 'd')
    {
        
$final array_reverse($finaltrue);
    }
    return 
$final;
}
?>

Example

array_natcase Can be used in the following way:

<?php
$array 
= array(
                
'a'=>array('x'=>10'other'=>'egh'),
                
'q'=>array('x'=>9'other'=>'blah'),
                
'7'=>array('x'=>1'other'=>'rawr'),
                
'6'=>array('x'=>22'other'=>'abcd'));
$array array_natcase('x'$array);
//outputs the array in the following order
// 7, q, a, 6 
?>

Added on Oct 20th at 2 am by Scott - 0 Comments


more_nums

Information

(PHP Version >= 3)
Functions used in this snippet: strlen, str_replace.

Description

bool more_nums( mixed str )

Returns true if the given string str contains more numbers than characters. Returns false if not.

Snippet

<?php
function more_nums($str){
    
$len strlen($str);
    
$str str_replace(array(0,1,2,3,4,5,6,7,8,9), ''$str);
    if( ( 
strlen($str) / $len ) < 0.5)
    {
        return 
true;
    }
    return 
false;
}
?>

Example

more_nums Can be used in the following way:

<?php
$str 
'abcd123';
if(
more_nums($str))
{
   echo 
'More Numbers';
}
else
{
   echo 
'Less Numbers';
}

$str 'ab12345cd6';
if(
more_nums($str))
{
   echo 
'More Numbers';
}
?>

Added on Oct 19th at 9 pm by Scott - 0 Comments
Updated on Oct 19th at 9 pm.


image_constraints

Information

(PHP Version >= 4.0.6)
Functions used in this snippet: imagesx, imagesy, imagecreatetruecolor, imagecopyresized.

Description

int image_constraints( int img, int dimx, int dimy )

Creates a resized image with the maximum width being dimx and height dimy without distorting the image.

Snippet

<?php
function image_constraints($img$dimx$dimy){
    
$x imagesx($img);
    
$y imagesy($img);
    
$xorg $x;
    
$yorg $y;
    
$altered false;
    if(
$x $dimx){
        
$ratio = ($dimx $x);
        
$x $ratio $x;
        
$y $ratio $y;
        
$altered true;
    }
    if(
$y $dimy){
        
$ratio = ($dimy $y);
        
$y $ratio $y;
        
$x $ratio $x;
        
$altered true;
    }
    if(!
$altered){
        return 
$img;
    }
    
$dest imagecreatetruecolor($x$y);
    
imagecopyresized($dest$img0000$x$y$xorg$yorg);
    return 
$dest;
}
?>

Example

image_constraints Can be used in the following way:

<?php
$img 
imagecreatefromjpeg('someimage.jpg'); // img size is 250 by 300 we will say
$img image_constraints($img100100);
imagejpeg($img); // output image is 83 by 100
?>

Added on Oct 9th at 7 pm by Scott - 4 Comments
Updated on May 14th at 3 pm.