PHP Code:
function capsfirst_sentence($string) {
return empty($string) ? '' : preg_replace('/\b(\w)/e', 'strtoupper("$1")', $string);
}
function capsfirst_sentence($string) {
return empty($string) ? '' : preg_replace('/\b(\w)/e', 'strtoupper("$1")', $string);
}
function count_files($dir_path = '', $what = '') {
$count = count(glob($dir_path.'/*.'.$what.''));
return echo 'There is '.($count > 1
? '<b>'.$count.'</b> files' : ($count == 1
? '<b>one</b> file' : '<b>no files</b>')).' in '.dirname($dir_path).'<br/>'.PHP_EOL;
}
//
count_files('downloads/images', 'jpg');
count_files('downloads/images', 'jpg|jpeg');
count_files('downloads/videos', '3gp');
count_files('downloads/videos', '3gp|mp4');
function random_readable_pwd($length=10){
// the wordlist from which the password gets generated
// (change them as you like)
$words = 'dog,cat,sheep,sun,sky,red,ball,happy,ice,';
$words .= 'green,blue,music,movies,radio,green,turbo,';
$words .= 'mouse,computer,paper,water,fire,storm,chicken,';
$words .= 'boot,freedom,white,nice,player,small,eyes,';
$words .= 'path,kid,box,black,flower,ping,pong,smile,';
$words .= 'coffee,colors,rainbow,plus,king,tv,ring';
// Split by ",":
$words = explode(',', $words);
if (count($words) == 0){ die('Wordlist is empty!'); }
// Add words while password is smaller than the given length
$pwd = '';
while (strlen($pwd) < $length){
$r = mt_rand(0, count($words)-1);
$pwd .= $words[$r];
}
// append a number at the end if length > 2 and
// reduce the password size to $length
$num = mt_rand(1, 99);
if ($length > 2){
$pwd = substr($pwd,0,$length-strlen($num)).$num;
} else {
$pwd = substr($pwd, 0, $length);
}
return $pwd;
}
function anti_xss( $string, $type )
{
switch ( $type ) {
case "int":
$string = ( int )$string ;
break ;
case "mysql":
$string = mysql_real_escape_string( $string ) ;
break ;
case "nohtml":
$string = htmlentities( $string , ENT_QUOTES, "UTF-8" ) ; // or ISO-8859-2 for romanian charset
break ;
}
return $string ;
}
//- int, use it for parameters with numbers. Example:
$uid = anti_xss($_GET["uid"], "int"); // profil.php?uid=25
//- mysql, use it for parameters with data base. Example:
$pass = anti_xss($_POST["pass"], "mysql"); // INSERT INTO `x_table` WHERE `pass`='".$pass."'
//- nohtml, use it for showing information on site. Example:
$text = "Playromania it kid and write <script>alert("Im nobody and a lost soul on the net :D");</script>";
echo anti_xss($text, "nohtml");
function console_log()
{
$args = func_get_args();
$argsSize = count($args);
if ($argsSize == 0)
{
return false;
}
if ($argsSize == 1)
{
$output = sprintf($args[0]);
}
else
{
$output = call_user_func_array('sprintf', $args);
}
$output = str_replace(array("\r", "\n"), '', $output);
echo '<script type="text/javascript"> if ( ! window.console) console = {}; console.log = console.log || function(name, data){}; console.log("'.$output.'"); </script>';
}
class Test
{
private $test = 2;
}
console_log(print_r(new Test(), true));
$day = 21;
console_log("%d %s %d", $day, 'Dec', 2004);
console_log("Code at line %d .", __LINE__);
console_log(print_r($days, true));
echo 'Hellow world!';
function dirmtime($dir) {
$last_modified = 0;
$files = glob($dir.'/*');
foreach ($files as $file) {
if (is_dir($file)) {
$modified = dirmtime($file);
} else {
$modified = filemtime($file);
}
if ($modified > $last_modified) {
$last_modified = $modified;
}
}
return $last_modified;
}
function random_color(){
mt_srand((double)microtime()*1000000);
$c = '';
while(strlen($c)<6){
$c .= sprintf("%02X", mt_rand(0, 255));
}
return $c;
}
random_color() // => returns something like: '7C42BA', '5F3964'
// local file that should be send to the client
$local_file = 'test-file.zip';
// filename that the user gets as default
$download_file = 'your-download-name.zip';
// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;
if(file_exists($local_file) && is_file($local_file)) {
// send headers
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: filename='.$download_file);
// flush content
flush();
// open file stream
$file = fopen($local_file, "r");
while (!feof($file)) {
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
sleep(1);
}
// close file stream
fclose($file);
}
else {
die('Error: The file '.$local_file.' does not exist!');
}
function cutout($string, $howmuch, $etc = false) {
return mb_strlen($string, 'UTF-8') > $howmuch
? mb_substr($string, 0, $howmuch, 'UTF-8').($etc !== false ? '...' : '') : $string;
}
$string = 'Some too long string';
$string = cutout($string, 10, true);
echo $string; //Some too l...
function openFile($file, $mode, $input, $marker = "") {
if ($mode == 'READ') {
if (is_readable($file)) {
$handle = @fopen($file, 'r');
$output = fread($handle, filesize($file));
return $output;
} else {
return false;
}
} elseif ($mode == 'ECHO') {
if (is_file($file)) {
return str_replace(PHP_EOL, "<br/>", htmlentities(file_get_contents($file), ENT_QUOTES | ENT_IGNORE, 'UTF-8'));
} else {
return false;
}
} elseif ($mode == 'WRITE') {
if (is_writeable($file) && !empty($input)) {
$handle = @fopen($file, 'w');
if (!fwrite($handle, $input)) {
return false;
} else {
return true;
}
} else {
return false;
}
} elseif ($mode == 'EDITBACK') {
if (is_file($file) && !empty($input)) {
$handle = @fopen($file, 'r+');
$read = fread($handle, filesize($file));
$data = $read.$input;
if (!fwrite($handle, $data)) {
return false;
} else {
return true;
}
} else {
return false;
}
} elseif ($mode == 'EDITBEGIN') {
if (is_file($file) && !empty($input)) {
$handle = @fopen($file, 'r+');
$read = fread($handle, filesize($file));
$data = $input.$read;
if (!fwrite($handle, $data)) {
return false;
} else {
return true;
}
} else {
return false;
}
} elseif ($mode == 'UPDATE') {
if (is_writeable($file) && !empty($input)) {
$marker = empty($marker) ? "\n" : $marker;
$handle = @fopen($file, 'a');
$data = $input.$marker;
if (!fwrite($handle, $data)) {
return false;
} else {
return true;
}
} else {
return false;
}
} elseif ($mode == 'CLEAR') {
if (is_file($file)) {
$handle = @fopen($file, 'a+');
if (ftruncate($handle, 0)) {
return true;
} else {
return false;
}
} else {
return false;
}
} elseif ($mode == 'DELFILE') {
if (is_file($file)) {
if (!unlink($file)) {
return false;
} else {
return true;
}
} else {
return false;
}
} elseif ($mode == 'CRTDIR') {
if (!empty($file) && !is_dir($file)) {
if (!mkdir($file)) {
return false;
} else {
return true;
}
} else {
return false;
}
} elseif ($mode == 'DELDIR') {
if (!empty($file) && is_dir($file)) {
if (!rmdir($file)) {
return false;
} else {
return true;
}
} else {
return false;
}
} else {
return false;
}
fclose($handle);
clearstatcache();
}
name@mail.com name.lastname@mail.com name.lastname@some-mail.com
function validemail($string) {
if (preg_match('/^[a-z0-9\._-]+@[a-z0-9\.-]+\.[a-z]{2,4}$/iD', $string)) {
return true;
} else {
return false;
}
}
function thetime($toset = 0) {
$thetime = time();
$toset = (int) $toset;
$set = $toset + 0;
return $thetime + ($set * 60 * 60);
}
function formatstamp($setformat = '', $stamp, $settime = 0) {
return date($setformat, $stamp + ($settime * 60 * 60));
}
echo formatstamp('d.m.Y H:i', thetime(), $sethours).'<br/>';
<?php
$fileName = '';
if ($fileName == NULL)
{
$fileName = isset($_GET['f']) ? $_GET['f'] : exit('<b>Usage: ?f=Filename.ext</b>');
}
$newLine = isset($_GET['nl']) ? TRUE : FALSE;
$f = file($fileName);
foreach ($f AS $k => $value)
{
$f[$k] = str_replace(PHP_EOL, '', $f[$k]);
if ($newLine)
{
$f[$k] = str_replace('{', PHP_EOL.'{'.PHP_EOL, $f[$k]);
$f[$k] = str_replace('}', PHP_EOL.'}'.PHP_EOL, $f[$k]);
}
if ($f[$k] == NULL)
unset($f[$k]);
}
file_put_contents(mt_rand(10000, 99999).'-'.$fileName, implode(PHP_EOL, $f));
?><?php
function format_number($numar, $separator = '.', $precizie = 3)
{
$numar = (string) $numar;
$cifre = strlen($numar);
$t = 1;
$numar_formatat = '';
for ($i = $cifre-1; $i>=0; $i--)
{
$numar_formatat = $numar[$i] . $numar_formatat;
if ($t % $precizie == 0 && isset($numar[$i-1]))
{
$numar_formatat = $separator . $numar_formatat;
$t = 1;
}
else
$t++;
}
return $numar_formatat;
}
echo format_number(3000000); // Will output : 3.000.000
echo format_number(3000000, ','); // Will output : 3,000,000
// ARGS: format_number(int/string number, separator, precision);
//PS: if you pass the number like an int make sure that it not contains more than 14 digits or by string with unlimited digits
?>function RegisterGlobalsOff()
{
foreach ($_REQUEST AS $key=>$value)
{
if( ! isset(${$key}))
{
${$key} = $value;
}
}
}
function openFile($file, $mode, $input) {
if ($mode == "READ") {
if (file_exists($file)) {
$handle = fopen($file, "r");
$output = fread($handle, filesize($file));
return $output; // output file text
} else {
return false; // failed.
}
} elseif ($mode == "WRITE") {
$handle = fopen($file, "w");
if (!fwrite($handle, $input)) {
return false; // failed.
} else {
return true; //success.
}
} elseif ($mode == "READ/WRITE") {
if (file_exists($file) && isset($input)) {
$handle = fopen($file, "r+");
$read = fread($handle, filesize($file));
$data = $read.$input;
if (!fwrite($handle, $data)) {
return false; // failed.
} else {
return true; // success.
}
} else {
return false; // failed.
}
} else {
return false; // failed.
}
fclose($handle);
}
openFile("files/text.txt", "WRITE", "Hello World!");
echo openFile("files/text.txt", "READ"); // OUTPUT > Hello World!
openFile("files/text.txt", "READ/WRITE", "!!");
echo openFile("files/text.txt", "READ"); // OUTPUT > Hello World!!!
elseif ($mode == 'UPDATE') {
$marker = "\n";
if (is_writeable($file) && isset($input)) {
$handle = @fopen($file, 'a');
$data = $input.$marker;
if (!fwrite($handle, $data)) {
return false;
} else {
return true;
}
} else {
return false;
}
}
Leave a comment: