Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-11-2013, 12:48 AM   PM User | #1
Dubz
Regular Coder

 
Join Date: Sep 2011
Posts: 206
Thanks: 15
Thanked 5 Times in 5 Posts
Dubz has a little shameless behaviour in the past
Thumbs up Basic PHP Functions

This just includes some basic functions for php that can help you out greatly. THis file can be found HERE to be included with your script if wanted. It is not recommended, but it is possible to eval(file_get_contents($link)); if you would like to have the dynamically updating version, but most people would not advise that as it imposes security issues since I could easily modify it and break into your scripts (which I wouldn't, I respect your security and wish the same to me).

Text version:
PHP Code:
<?php
##########################
#### Created By: Dubz ####
##########################

  ########################
#### This file contains ####
####  a few basic php   ####
####     functions      ####
  ########################

//Define BASICFUNCTIONS so users can add a check for this file and avoid calling it twice
define("BASICFUNCTIONS"true);

/*
* @credit 3nvisi0n
*
* Searches the given string and returns the inside of the left and right paremeters (case-sensitive)
*
* @return A string of text between $deliLeft and $deliRight
*/
function strbet($inputstr$deliLeft$deliRight)
{
    
$posLeft strpos($inputstr$deliLeft) + strlen($deliLeft);
    
$posRight strpos($inputstr$deliRight$posLeft);
    return 
substr($inputstr$posLeft$posRight $posLeft);
}


/*
* @credit 3nvisi0n
*
* Searches the given string and returns the inside of the left and right paremeters (case-insensitive)
*
* @return A string of text between $deliLeft and $deliRight
*/
function stribet($inputstr$deliLeft$deliRight)
{
    
$posLeft stripos($inputstr$deliLeft) + strlen($deliLeft);
    
$posRight stripos($inputstr$deliRight$posLeft);
    return 
substr($inputstr$posLeft$posRight $posLeft);
}


/*
* @credit 3nvisi0n
*
* Opens a webpage and grabs the sites source code (html)
*
* @param $includeHeaders determines if you want headers included in the results
* @return A string of the source code
*/
function get($url$includeHeaders false)
{
    
$urlp parse_url($url);
    
$fp fsockopen($urlp['host'], 80);
    
$path explode('/'$url4);
    
$cp count($path);
    
$path = ($cp >= 4) ? $path[3] : "";
    
$req "GET /$path HTTP/1.0\r\n";
    
$req .= "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
    
$req .= "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
    
$req .= "Host: $urlp[host]\r\n";
    
$req .= "Accept-Language: en-us,en;q=0.5\r\n";
    
$req .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0\r\n";
    
$req .= "Connection: close\r\n\r\n";
    
fputs($fp$req);
    
stream_set_timeout($fp,4);
    
$res stream_get_contents($fp);
    
fclose($fp);
    if(
$includeHeaders)
        return 
$res;
    
$res explode("\r\n\r\n"$res2);
    return 
$res[1];
}


/*
* @credit 3nvisi0n
*
* Posts data to a website using the curl method
*
* @param $url The URL to post to
* @param $postData The data to be posted
* @return An array of strings containing the status and content (html)
*/
function curl_post($url$postData)
{
    if(!isset(
$timeout))
        
$timeout 30;
    
$curl curl_init();
    if(
$curl)
    {
        
curl_setopt($curlCURLOPT_URL$url);
        
curl_setopt($curlCURLOPT_TIMEOUT$timeout);
        
curl_setopt($curlCURLOPT_USERAGENT'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)');
        
curl_setopt($curlCURLOPT_HEADERfalse);
        
curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
        
curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse);
        
curl_setopt($curlCURLOPT_SSL_VERIFYHOSTfalse);
        
curl_setopt($curlCURLOPT_POSTtrue);
        
curl_setopt($curlCURLOPT_POSTFIELDS$postData);
        
curl_setopt($curlCURLOPT_HTTPHEADER,
        array(
"Content-type: application/x-www-form-urlencoded"));
        
$html curl_exec($curl);
    }
    else
    {
        return array(
            
"status" => "error",
        );
    }
    
curl_close($curl);
    return array(
        
"status" => "ok",
        
"content" => $html
    
);
}


/*
* @credit Dubz
* Determines if the file is ran by command prompt
*
* @return A boolean regarding execution by command line
*/
function commandLine()
{
    return (
php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']));
}


/*
* @credit Dubz
*
* Gets the IP of the user requesting the file
*
* @return A string containing the user's IP
*/
function getIP()
{
    if(
key_exists('HTTP_X_FORWARDED_FOR'$_SERVER))
        
$ip $_SERVER['HTTP_X_FORWARDED_FOR'];
    else
        
$ip $_SERVER['REMOTE_ADDR'];
    
$ip trim($ip);
    if(
$ip == '::1')
        
$ip 'localhost';
    return 
$ip;
}


/*
* @credit Dubz
*
* Generates a random string
*
* @param $length The length of the string to return
* @param $lower Include lower-case characters
* @param $upper Include upper-case characters
* @param $numeric Include numeric characters
* @return A randomly generated string or false if empty
*/
function generateRandom($length 8$lower true$upper true$numeric true)
{
    
$length = (String)floor($length);
    if(!
ctype_digit($length) || $length 1)
        return 
false;
    
$array = array();
    
$string '';
    if(
$lower)
        
$array array_merge($arrayrange('a''z'));
    if(
$upper)
        
$array array_merge($arrayrange('A''Z'));
    if(
$numeric)
        
$array array_merge($arrayrange('0''9'));
    if(empty(
$array))
        return 
null;
    while(
strlen($string) < $length)
    {
        
$string .= $array[array_rand($array)];
    }
    return 
$string;
}


/*
* @credit Dubz
*
* Converts a numerical time() to a 'datetime' format
*
* @return A string with mysql 'datetime' stamp
*/
function mysql_datetime($time null)
{
    if(!
$time)
        
$time time();
    
$stamp date("Y-m-d H:i:s"$time);
    return 
$stamp;
}


/*
* @credit Dubz
*
* Opens and saves an image from a website or local location
*
* @param $inPath Location to get image
* @param $directory Location to save image
* @return Returns a booolean of if the image was saved
*/
function save_image($inPath$directory '')
{
    
//Download images from remote server
    
$file explode('/'$inPath);
    
$file array_pop($file);
    if(
substr($directory, -1) != '/' || empty($directory))
        
$directory .= '/';
    if(!
is_dir($directory))
        
mkdir($directory);
    
$outPath $directory.$file;
    
$in = @fopen($inPath"rb");
    if(
$in)
    {
        
$out fopen($outPath"wb");
        while (
$chunk fread($in8192))
        {
            
fwrite($out$chunk8192);
        }
        
fclose($in);
        
fclose($out);
        return 
true;
    }
    else
        return 
false;
}


/*
* @credit Dubz
*
* Checks if the given string is base64 valid
*
* @param $base64String String to be tesed for base64 validity
* @return Returns a booolean of if it is base64 valid
*/
function is_base64($base64String)
{
    return (
base64_encode(base64_decode($base64String)) == $base64String);
}


/*
* @credit Dubz
*
* Checks if the port is open for the ip address
*
* @param $ip IP adress of server checking
* @param $port Port number to check if open
* @return Returns a boolean of the port's status
*/
function checkPort($ip$port 80$timeout 2)
{
    if(!
$ip || !$port)
        return 
false;
    
$conn = @fsockopen($ip$port$errno$errstr$timeout);
    if(
$conn)
    {
        
fclose($conn);
        return 
true;
    }
}


/*
* @credit Dubz
*
* Turns an array into a string to be saved to a php file
* Used to save an array to a php file instead of a text file, which will not show up if ran
*
* @param $array Array to be converted to string
* @param $arrayName Name to be used for array in return
* @param $spaceTabs Uses 4 spaces instead of a tab
* @param $tabPos How far the array should be tabbed over
* @return Returns a string of a php array in standard php format
*/
function array2Text($array$arrayName 'array'$spaceTabs false$startingTab 0$tabsOver 0)
{
    
#Handle errors before possibly overwriting data
    
if(!is_array($array))
    {
        
$backtrace debug_backtrace();
        die(
"<b>Warning</b>: ".$backtrace[0]['function']."() expects parameter 1 to be array, ".gettype($backtrace[0]['argv'][0])." given in <b>".$backtrace[0]['file']."</b> on line <b>".$backtrace[0]['line']."</b>");
    }
    
$tab = ($spaceTabs) ? '    ' "\t";
    
#Start the array
    
$text '';
    if(
$tabsOver == 0)
    {
        for(
$i 0$i $startingTab$i++)
        {
            
$text .= $tab;
        }
        
$text .= '$'.$arrayName.' = ';
    }
    
#Declare an array as the value
    
$text .= 'array(';
    
#End array on the same line, no need to expand it if it's empty
    
if(empty($array))
    {
        
#End the array
        
if($tabsOver == 0)
            
$text .= ');';
        
#Another key is after, just place a comma
        
else
            
$text .= '),';
    }
    
#Expand the array and include data inside
    
else
    {
        
$text .= "\n";
        
#Insert the array as key -> value
        
foreach($array as $key => $value)
        {
            
#Escape any apostrophes in the key
            
$key str_replace('\'''\\\''$key);
            for(
$i 0$i <= ($startingTab $tabsOver); $i++)
            {
                
$text .= $tab;
            }
            if(
is_int($key))
                
$text .= "$key => ";
            else
                
$text .= "'$key' => ";
            if(
is_array($value))
                
$text .= array2Text($value$arrayName$spaceTabs$startingTab$tabsOver+1);
            elseif(
is_int($value))
                
$text .= "$value,";
            else
            {
                
#Escape any apostrophes in the value
                
$value str_replace('\'''\\\''$value);
                
$text .= "'$value',";
            }
            
$text .= "\n";
        }
        
#Trim the last comma from the array
        
$text substr($text0, -2)."\n";
        
#Add spacing for the closing parenthesis
        
for($i 0$i < ($startingTab $tabsOver); $i++)
        {
            
$text .= $tab;
        }
        
#End the array
        
if($tabsOver == 0)
            
$text .= ');';
        
#Another key is after, just place a comma
        
else
            
$text .= '),';
    }
    return 
$text;
}


/*
* @credit Dubz
*
* Checks if the user is on a proxy network
* NOTE: This is not 100% guaranteed and may return false positives if the client is running a webserver
*
* @return A boolean regarding if they are on a proxy
*/
function usingProxy()
{
    return 
checkPort(getIP(), 80);
}
?>
Dubz is offline   Reply With Quote
Old 02-20-2013, 04:54 AM   PM User | #2
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
And lot of these are a huge overkill. Case in point: array2Text():
PHP Code:
function array2Text$array$name 'array' )
{
    return 
'$' $name ' = 'var_export$arrayTRUE ) . ';';

__________________
ZCE
kbluhm is offline   Reply With Quote
Old 02-21-2013, 02:43 AM   PM User | #3
Dubz
Regular Coder

 
Join Date: Sep 2011
Posts: 206
Thanks: 15
Thanked 5 Times in 5 Posts
Dubz has a little shameless behaviour in the past
Quote:
Originally Posted by kbluhm View Post
And lot of these are a huge overkill. Case in point: array2Text():
PHP Code:
function array2Text$array$name 'array' )
{
    return 
'$' $name ' = 'var_export$arrayTRUE ) . ';';

Thanks for the tip. Although I've been coding in PHP for a few years now, I still don't know all the functions (especially since I've taught myself the whole way through). I'll definitely have to update that on the file and use it instead. It was nice for me to make it though since it gave me a challenge and some practice on using recursion which is also a good thing, but every coder knows resources are important!
Dubz is offline   Reply With Quote
Old 02-28-2013, 08:34 PM   PM User | #4
Dubz
Regular Coder

 
Join Date: Sep 2011
Posts: 206
Thanks: 15
Thanked 5 Times in 5 Posts
Dubz has a little shameless behaviour in the past
The code that is listed in this post is current, however the link is not currently working. I will try to get a working link soon for anyone who cares for updates for it (not too often).

Last edited by Dubz; 03-02-2013 at 07:38 AM..
Dubz is offline   Reply With Quote
Old 04-06-2013, 03:50 AM   PM User | #5
Dubz
Regular Coder

 
Join Date: Sep 2011
Posts: 206
Thanks: 15
Thanked 5 Times in 5 Posts
Dubz has a little shameless behaviour in the past
I have created a couple more functions and added them to the list for those who would like them. If you would like to stay updated, you can use the link here for now but I will try to keep updating it here as well (I don't add too many functions that often). Please don't hotlink to the file as it is on free hosting and I don't have unlimited bandwidth. If anyone knows of a place this can be hosted and linked to (without the link changing on updates) for free and has a good uptime, please let me know.

PHP Code:
<?php
##########################
#### Created By: Dubz ####
##########################

  ########################
#### This file contains ####
####  a few basic php   ####
####     functions      ####
  ########################

//Define BASICFUNCTIONS so users can add a check for this file and avoid calling it twice
define("BASICFUNCTIONS"true);

/*
* @credit 3nvisi0n
*
* Searches the given string and returns the inside of the left and right paremeters (case-sensitive)
*
* @return A string of text between $deliLeft and $deliRight
*/
function strbet($inputstr$deliLeft$deliRight)
{
    
$posLeft strpos($inputstr$deliLeft) + strlen($deliLeft);
    
$posRight strpos($inputstr$deliRight$posLeft);
    return 
substr($inputstr$posLeft$posRight $posLeft);
}


/*
* @credit 3nvisi0n
*
* Searches the given string and returns the inside of the left and right paremeters (case-insensitive)
*
* @return A string of text between $deliLeft and $deliRight
*/
function stribet($inputstr$deliLeft$deliRight)
{
    
$posLeft stripos($inputstr$deliLeft) + strlen($deliLeft);
    
$posRight stripos($inputstr$deliRight$posLeft);
    return 
substr($inputstr$posLeft$posRight $posLeft);
}


/*
* @credit 3nvisi0n
*
* Opens a webpage and grabs the sites source code (html)
*
* @param $includeHeaders determines if you want headers included in the results
* @return A string of the source code
*/
function get($url$includeHeaders false)
{
    
$urlp parse_url($url);
    
$fp fsockopen($urlp['host'], 80);
    
$path explode('/'$url4);
    
$cp count($path);
    
$path = ($cp >= 4) ? $path[3] : "";
    
$req "GET /$path HTTP/1.0\r\n";
    
$req .= "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
    
$req .= "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
    
$req .= "Host: $urlp[host]\r\n";
    
$req .= "Accept-Language: en-us,en;q=0.5\r\n";
    
$req .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0\r\n";
    
$req .= "Connection: close\r\n\r\n";
    
fputs($fp$req);
    
stream_set_timeout($fp,4);
    
$res stream_get_contents($fp);
    
fclose($fp);
    if(
$includeHeaders)
        return 
$res;
    
$res explode("\r\n\r\n"$res2);
    return 
$res[1];
}


/*
* @credit jmj001
*
* Posts data to a website using the curl method
*
* @param $url The URL to post to
* @param $postData The array of data to be posted
* @return An array of strings containing the status and content (html)
*/
function curl_post($url$postData)
{
    if(!isset(
$timeout))
        
$timeout 30;
    
$curl curl_init();
    if(
$curl)
    {
        
curl_setopt($curlCURLOPT_URL$url);
        
curl_setopt($curlCURLOPT_TIMEOUT$timeout);
        
curl_setopt($curlCURLOPT_USERAGENT'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)');
        
curl_setopt($curlCURLOPT_HEADERfalse);
        
curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
        
curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse);
        
curl_setopt($curlCURLOPT_SSL_VERIFYHOSTfalse);
        
curl_setopt($curlCURLOPT_POSTtrue);
        
curl_setopt($curlCURLOPT_POSTFIELDS$postData);
        
curl_setopt($curlCURLOPT_HTTPHEADER,
        array(
"Content-type: application/x-www-form-urlencoded"));
        
$html curl_exec($curl);
    }
    else
    {
        return array(
            
"status" => "error",
        );
    }
    
curl_close($curl);
    return array(
        
"status" => "ok",
        
"content" => $html
    
);
}


/*
* @credit Dubz
* Determines if the file is ran by command prompt
*
* @return A boolean regarding execution by command line
*/
function commandLine()
{
    return (
php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']));
}


/*
* @credit Dubz
*
* Gets the IP of the user requesting the file
*
* @return A string containing the user's IP
*/
function getIP()
{
    if(
key_exists('HTTP_CF_CONNECTING_IP'$_SERVER))       #Cloudflare handler
        
$ip $_SERVER['HTTP_CF_CONNECTING_IP'];
    elseif(
key_exists('HTTP_X_FORWARDED_FOR'$_SERVER))
        
$ip $_SERVER['HTTP_X_FORWARDED_FOR'];
    else
        
$ip $_SERVER['REMOTE_ADDR'];
    
$ip trim($ip);
    if(
$ip == '::1')
        
$ip 'localhost';
    return 
$ip;
}


/*
* @credit Dubz
*
* Generates a random string
*
* @param $length The length of the string to return
* @param $lower Include lower-case characters
* @param $upeer Include upper-case characters
* @param $numeric Include numeric characters
* @return A randomly generated string or false if empty
*/
function generateRandom($length 8$lower true$upper true$numeric true)
{
    
$length = (String)floor($length);
    if(!
ctype_digit($length) || $length 1)
        return 
false;
    
$array = array();
    
$string '';
    if(
$lower)
        
$array array_merge($arrayrange('a''z'));
    if(
$upper)
        
$array array_merge($arrayrange('A''Z'));
    if(
$numeric)
        
$array array_merge($arrayrange('0''9'));
    if(empty(
$array))
        return 
null;
    while(
strlen($string) < $length)
    {
        
$string .= $array[array_rand($array)];
    }
    return 
$string;
}


/*
* @credit Dubz
*
* Converts a numerical time() to a 'datetime' format
*
* @return A string with mysql 'datetime' stamp
*/
function mysql_datetime($time null)
{
    if(!
$time)
        
$time time();
    
$stamp date("Y-m-d H:i:s"$time);
    return 
$stamp;
}


/*
* @credit Dubz
*
* Opens and saves an image from a website or local location
*
* @param $inPath Location to get image
* @param $directory Location to save image
* @return Returns a boolean of if the image was saved
*/
function save_image($inPath$directory '')
{
    
//Download images from remote server
    
$file explode('/'$inPath);
    
$file array_pop($file);
    if(
substr($directory, -1) != '/' || empty($directory))
        
$directory .= '/';
    if(!
is_dir($directory))
        
mkdir($directory);
    
$outPath $directory.$file;
    
$in = @fopen($inPath"rb");
    if(
$in)
    {
        
$out fopen($outPath"wb");
        while (
$chunk fread($in8192))
        {
            
fwrite($out$chunk8192);
        }
        
fclose($in);
        
fclose($out);
        return 
true;
    }
    else
        return 
false;
}


/*
* @credit Dubz
*
* Checks if the given string is base64 valid
*
* @param $base64String String to be tesed for base64 validity
* @return Returns a booolean of if it is base64 valid
*/
function is_base64($base64String)
{
    return (
base64_encode(base64_decode($base64String)) == $base64String);
}


/*
* @credit Dubz
*
* Checks if the port is open for the ip address
*
* @param $ip IP adress of server checking
* @param $port Port number to check if open
* @return Returns a boolean of the port's status
*/
function checkPort($ip$port 80$timeout 2)
{
    if(!
$ip || !$port)
        return 
false;
    
$conn = @fsockopen($ip$port$errno$errstr$timeout);
    if(
$conn)
    {
        
fclose($conn);
        return 
true;
    }
}


/*
* @credit Dubz
*
* Turns an array into a string to be saved to a php file
* Used to save an array to a php file instead of a text file, which will not show up if ran
*
* @param $array Array to be converted to string
* @param $arrayName Name to be used for array in return
* @param $spaceTabs Uses 4 spaces instead of a tab
* @param $tabPos How far the array should be tabbed over
* @return Returns a string of a php array in standard php format
*/
/*function array2Text($array, $arrayName = 'array', $spaceTabs = false, $startingTab = 0, $tabsOver = 0)
{
    #Handle errors before possibly overwriting data
    if(!is_array($array))
    {
        $backtrace = debug_backtrace();
        die("<b>Warning</b>: ".$backtrace[0]['function']."() expects parameter 1 to be array, ".gettype($backtrace[0]['argv'][0])." given in <b>".$backtrace[0]['file']."</b> on line <b>".$backtrace[0]['line']."</b>");
    }
    $tab = ($spaceTabs) ? '    ' : "\t";
    #Start the array
    $text = '';
    if($tabsOver == 0)
    {
        for($i = 0; $i < $startingTab; $i++)
        {
            $text .= $tab;
        }
        $text .= '$'.$arrayName.' = ';
    }
    #Declare an array as the value
    $text .= 'array(';
    #End array on the same line, no need to expand it if it's empty
    if(empty($array))
    {
        #End the array
        if($tabsOver == 0)
            $text .= ');';
        #Another key is after, just place a comma
        else
            $text .= '),';
    }
    #Expand the array and include data inside
    else
    {
        $text .= "\n";
        #Insert the array as key -> value
        foreach($array as $key => $value)
        {
            #Escape any apostrophes in the key
            $key = str_replace('\'', '\\\'', $key);
            for($i = 0; $i <= ($startingTab + $tabsOver); $i++)
            {
                $text .= $tab;
            }
            if(is_int($key))
                $text .= "$key => ";
            else
                $text .= "'$key' => ";
            if(is_array($value))
                $text .= array2Text($value, $arrayName, $spaceTabs, $startingTab, $tabsOver+1);
            elseif(is_int($value))
                $text .= "$value,";
            else
            {
                #Escape any apostrophes in the value
                $value = str_replace('\'', '\\\'', $value);
                $text .= "'$value',";
            }
            $text .= "\n";
        }
        #Trim the last comma from the array
        $text = substr($text, 0, -2)."\n";
        #Add spacing for the closing parenthesis
        for($i = 0; $i < ($startingTab + $tabsOver); $i++)
        {
            $text .= $tab;
        }
        #End the array
        if($tabsOver == 0)
            $text .= ');';
        #Another key is after, just place a comma
        else
            $text .= '),';
    }
    return $text;
}*/


/*
* @credit kbluhm
*
* Turns an array into a string to be saved to a php file
* Used to save an array to a php file instead of a text file, which will not show up if ran
*
* @param $array Array to be converted to string
* @param $arrayName Name to be used for array in return
* @return Returns a string of a php array in standard php format
*
*/
function array2Text($array$name 'array')
{
    
$text '$'.$name.' = '.var_export($arraytrue).';';
}

/*
* @credit Dubz
*
* Checks if the user is on a proxy network
* NOTE: This is not 100% guaranteed and may return false positives if the client is running a webserver
*
* @return A boolean regarding if they are on a proxy
*/
function usingProxy()
{
    
$blockedPorts = array('80''443');
    foreach(
$blockedPorts as $port)
    {
        if(
checkPort(getIP(), $port))
            return 
true;
    }
    return 
false;
}


/*
* @credit Dubz
*
* Converts an array to a comma separated string following proper grammar rules
*
* @param $list The array to list
* @param $lastIdentifier The word used before the last list item
* @return A string of a properly formatted, comma separated list
*/
function array2commaList($list$lastIdentifier 'and')
{
    switch(
count($list))
    {
        case 
0:
            
$string false;
        break;
        case 
1:
            
$string $list[0];
        break;
        case 
2:
            
$string implode(' '.$lastIdentifier.' '$list);
        break;
        default:
            
$lastItem array_pop($list);
            
$string implode(', '$list).', '.$lastIdentifier.' '.$lastItem;
    }
    return 
$string;
}


/*
* @credit Dubz
*
* Converts error numbrs to the string used to define them
*
* @param $errno The error number provided
* @return A string that is used to define the error number
*/
function errtostr($errno)
{
    switch(
$errno)
    {
        case 
E_ERROR: return 'E_ERROR'; break;
        case 
E_WARNING: return 'E_WARNING'; break;
        case 
E_PARSE: return 'E_PARSE'; break;
        case 
E_NOTICE: return 'E_NOTICE'; break;
        case 
E_CORE_ERROR: return 'E_CORE_ERROR'; break;
        case 
E_CORE_WARNING: return 'E_COMPILE_ERROR'; break;
        case 
E_COMPILE_WARNING: return 'E_COMPILE_WARNING'; break;
        case 
E_USER_ERROR: return 'E_USER_ERROR'; break;
        case 
E_USER_WARNING: return 'E_USER_WARNING'; break;
        case 
E_USER_NOTICE: return 'E_USER_NOTICE'; break;
        case 
E_STRICT: return 'E_STRICT'; break;
        case 
E_RECOVERABLE_ERROR: return 'E_RECOVERABLE_ERROR'; break;
        case 
E_DEPRECATED: return 'E_DEPRECATED'; break;
        case 
E_USER_DEPRECATED: return 'E_USER_DEPRECATED'; break;
        case 
E_ALL: return 'E_ALL'; break;
        default:
            return 
false;
    }
}


/*
* @Credit Dubz
*
* Tells if a number is within the given range
*
* @param $num The number to check
* @param $low The lower number of the range
* @param $high The higher number of the range
* @return A boolen telling if the number is in the range
*/
function in_range($num$low$high)
{
    return ((
$num >= $low) && ($num <= $high));
}


/*
* @Credit Fou-Lu
* @Credit Dubz
*
* Grabs all of the input values of the html code given
*
* @param $html The code of an html file
* @return An array of the names and values of the code
*/
function get_input_values($html)
{
    
$dom = new DOMDocument('1.0');
    
$dom->loadHTML($html);
    
$aResult = array();
    if(
$input $dom->getElementsByTagName('input'))
    {
        foreach(
$input as $item)
        {
            
$attributes $item->attributes;
            if(
$attributes->getNamedItem('name') != null)
            {
                
$aResult[$attributes->getNamedItem('name')->nodeValue] = array();
                foreach(
$attributes as $attr)
                {
                    
$aResult[$attributes->getNamedItem('name')->nodeValue][$attr->name] = $attr->value;
                }
            }
        }
    }
    return 
$aResult;
}
?>

Last edited by Dubz; 04-07-2013 at 06:09 AM..
Dubz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:14 PM.


Advertisement
Log in to turn off these ads.