Skip to content

Instantly share code, notes, and snippets.

@justinkelly
justinkelly / strstrbi.php
Created November 17, 2010 09:46
kick ass strstr
<?php
function strstrbi($haystack, $needle, $before_needle=FALSE, $include_needle=TRUE, $case_sensitive=FALSE) {
//Find the position of $needle
if($case_sensitive) {
$pos=strpos($haystack,$needle);
} else {
$pos=strpos(strtolower($haystack),strtolower($needle));
}
@justinkelly
justinkelly / recursive_zip.php
Created November 17, 2010 09:43
recursively zip a folder with php
<?php
function Zip($source, $destination)
{
if (extension_loaded('zip') === true)
{
if (file_exists($source) === true)
{
$zip = new ZipArchive();
@justinkelly
justinkelly / recursive_copy.php
Created November 17, 2010 09:45
recursive copy for php
<?php
function recurse_copy($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
@justinkelly
justinkelly / date_diff_days.php
Created January 6, 2011 09:43
find the difference between 2 dates in days
<?php
function date_diff_in_days($from, $to = 'now')
{
$date1 = strtotime($from);
$date2 = strtotime($to);
$dateDiff = $date1 - $date2;
return floor($dateDiff/(60*60*24));
}
@justinkelly
justinkelly / Date_diff_long.php
Created January 6, 2011 10:02
Find the difference in years, months, weeks and days between to dates
function date_diff_long($from, $to = null)
{
$to = (($to === null) ? (time()) : ($to));
$to = ((is_int($to)) ? ($to) : (strtotime($to)));
$from = ((is_int($from)) ? ($from) : (strtotime($from)));
$units = array
(
"year" => 29030400, // seconds in a year (12 months)
"month" => 2419200, // seconds in a month (4 weeks)
@justinkelly
justinkelly / stripNonNumeric.php
Created May 3, 2011 12:06
Remove non-numeric characters from a string.
<?php
/*
Remove non-numeric characters from a string.
*/
function stripNonNumeric($str='') {
return preg_replace('(\D+)', '', $str);
}
?>
@justinkelly
justinkelly / strip_null_array_entries.php
Created July 3, 2011 10:26
PHP function to strip null array entries
<?php
/*
* use this function to strip null array entries from an array
* useful when doing a db update with pdo/zend_db_table etc..
*/
function strip_null_array_entries($values){
$array = array();
foreach($values as $key=>$value){
<?php
//db update query from form with image upload fields
$data = array(
'company_name' => $form->getValue('company_name'),
'company_image' => $form->getElement('company_image')->getValue(),
'company_logo' => $form->getElement('company_logo')->getValue(),
'website' => $form->getValue('website')
);
@justinkelly
justinkelly / User.php
Created September 1, 2011 23:21
Simple Password confirmation with Zend_Form
<?php
class Application_Form_User extends Zend_Form
{
public function init()
{
/* Form Elements & Other Definitions Here ... */
$this->setName('user');
@justinkelly
justinkelly / base64_url.php
Created September 15, 2011 13:48
simple base64 encode/decode url safe functions
<?php
function base64_url_encode($input)
{
return strtr(base64_encode($input), '+/=', '-_,');
}
function base64_url_decode($input)
{
return base64_decode(strtr($input, '-_,', '+/='));