Last active
August 29, 2015 14:06
-
-
Save vienhoang/15c5e24bafbe216c4351 to your computer and use it in GitHub Desktop.
PHP: Misc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PHP Misc | |
================================================================================================ | |
Difference between two dates | |
$date1 = new DateTime('2007-03-24'); | |
$date2 = new DateTime('2009-06-26'); | |
$interval = $date1->diff($date2); | |
echo 'difference: ' . $interval->y . ' years, ' . $interval->m.' months, '.$interval->d.' days '; | |
--- | |
Trim ending slashes | |
$string = 'http://google.com/'; | |
if(substr($string, -1) == '/') { | |
$string = substr($string, 0, -1); | |
} | |
// | |
$string = rtrim($string, '/'); | |
echo $string; | |
--- | |
Check if string contains substring | |
if( strpos( $string, $substring ) === false ) { // substring not found in string | |
// code | |
} else { // substring found in string | |
// code | |
} | |
--- | |
Create zip file | |
/* creates a compressed zip file */ | |
function create_zip($files = array(),$destination = '',$overwrite = false) { | |
//if the zip file already exists and overwrite is false, return false | |
if(file_exists($destination) && !$overwrite) { return false; } | |
//vars | |
$valid_files = array(); | |
//if files were passed in... | |
if(is_array($files)) { | |
//cycle through each file | |
foreach($files as $file) { | |
//make sure the file exists | |
if(file_exists($file)) { | |
$valid_files[] = $file; | |
} | |
} | |
} | |
//if we have good files... | |
if(count($valid_files)) { | |
//create the archive | |
$zip = new ZipArchive(); | |
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { | |
return false; | |
} | |
//add the files | |
foreach($valid_files as $file) { | |
$zip->addFile($file,$file); | |
} | |
//debug | |
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; | |
//close the zip -- done! | |
$zip->close(); | |
//check to make sure the file exists | |
return file_exists($destination); | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
// Usage | |
$files_to_zip = array( | |
'preload-images/1.jpg', | |
'preload-images/2.jpg', | |
'preload-images/5.jpg', | |
'kwicks/ringo.gif', | |
'rod.jpg', | |
'reddit.gif' | |
); | |
//if true, good; if false, zip creation failed | |
$result = create_zip($files_to_zip,'my-archive.zip'); | |
--- | |
Even or Odd | |
if ($i % 2) { | |
echo "$i is odd"; | |
} else { | |
echo "$i is even"; | |
} | |
--- | |
Remove trailing zeros of a decimal number | |
function clean_num( $num ){ | |
$pos = strpos($num, '.'); | |
if($pos === false) { // it is integer number | |
return $num; | |
}else{ // it is decimal number | |
return rtrim(rtrim($num, '0'), '.'); | |
} | |
} | |
echo clean_num(100.02); | |
--- | |
array_map, array_walk and array_filter | |
$origarray1 = array(2.4, 2.6, 3.5); | |
$origarray2 = array(2.4, 2.6, 3.5); | |
print_r(array_map('floor', $origarray1)); // $origarray1 stays the same | |
// changes $origarray2 | |
array_walk($origarray2, function (&$v, $k) { $v = floor($v); }); | |
print_r($origarray2); | |
// this is a more proper use of array_walk | |
array_walk($origarray1, function ($v, $k) { echo "$k => $v", "\n"; }); | |
// array_map accepts several arrays | |
print_r( | |
array_map(function ($a, $b) { return $a * $b; }, $origarray1, $origarray2) | |
); | |
// select only elements that are > 2.5 | |
print_r( | |
array_filter($origarray1, function ($a) { return $a > 2.5; }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment