Skip to content

Instantly share code, notes, and snippets.

@sepehr
sepehr / uk_postcode_validator.php
Last active July 10, 2022 08:31
PHP: UK Postcode Validation
/**
* Custom validation callback to validate UK postcodes.
*
* It also tries to format provided postcode in correct format.
*
* Note: It's only usable for "postcode" fields.
*/
public function check_postcode_uk($original_postcode)
{
// Set callback's custom error message (CI specific)
@sepehr
sepehr / array_map_assoc.php
Created September 1, 2012 02:56
PHP: Array Map Associative
<?php
/**
* Converts a linear array to its associative equivalent.
*
* @param $array
* Linear array to process.
* @param $function
* Callback name to call against each value.
*
* @return array
@sepehr
sepehr / get_month_diff.php
Last active May 4, 2020 23:59
PHP: Get month difference between two timestamps
<?php
/**
* Calculates how many months is past between two timestamps.
*
* @param int $start Start timestamp.
* @param int $end Optional end timestamp.
*
* @return int
*/
@sepehr
sepehr / gitignore2svnignore.sh
Created June 24, 2014 08:30 — forked from iegik/gitignore2svnignore.sh
Shell: Convert .svnignore to .gitignore & viceversa
#!/bin/bash
cat .gitignore | sed 's/^/\.\//g;s/\(.*\)\/\([0-9a-zA-Z\*\?\.]*\)$/svn propedit svn:ignore "\2" \1 /mg' | bash
@sepehr
sepehr / valid_email.php
Last active August 30, 2019 07:57
PHP: Email Validation (RFC 2822)
/**
* Verifies the syntax of the given e-mail address.
*
* See RFC 2822 for details.
*
* @param $mail
* A string containing an e-mail address.
*
* @return
* 1 if the email address is valid, 0 if it is invalid or empty, and FALSE if
@sepehr
sepehr / truncate.php
Created August 27, 2013 09:14
PHP: truncate()
<?php
/**
* Truncates string to the specified length.
*
* @param string $string String to truncate.
* @param integer $len Desired length.
* @param boolean $wordsafe Whether to truncate on word boundries or not.
* @param boolean $dots Whether to add dots or not.
*
@sepehr
sepehr / object_to_array.php
Created August 27, 2012 08:53
PHP: Recursively cast an object into array
<?php
/**
* Recursively casts an object into array.
*
* @param $object
* Object to cast to array.
*
* @return array
*/
@sepehr
sepehr / rmdir_recursive.php
Last active July 9, 2018 00:52
PHP: Recursive directory removal
<?php
/**
* Recursively removes a directory.
*
* @param string $path Directory path to remove.
* @param bool $suicide Whether to remove itself or not.
*
* @return void
*/
@sepehr
sepehr / mobile_detect.php
Created August 16, 2012 15:55
PHP: Mobile Browser Detection
<?php
/**
* Mobile Browser Detection for PHP.
*/
$useragent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/android.+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|meego.+mobile|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro
@sepehr
sepehr / osx-kong.sh
Last active June 12, 2018 01:54
OSX: Kong Installation
#!/bin/bash
#
# The homebrew formula of kong has a lot of version incompatibilities. So
# we install kong directly from Luarocks.
#
# Kong only works with Cassandra 2.1.x/2.2.x, the latest brew formula for
# cassandra is 3.x. We need to tap homebrew/versions and install cassandra22
# instead.
#