Skip to content

Instantly share code, notes, and snippets.

@sepehr
sepehr / valid_email.php
Last active Aug 30, 2019
PHP: Email Validation (RFC 2822)
View valid_email.php
/**
* 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 Aug 27, 2013
PHP: truncate()
View truncate.php
<?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 / get_daterange_timestamps.php
Created Aug 27, 2013
PHP: Get corresponding timestamp boundaries of a daterange
View get_daterange_timestamps.php
<?php
/**
* Generates timestamp bounderies for the passed date range name.
*
* @param string $range Date range name.
*
* @return array
*/
function get_daterange_timestamps($range)
@sepehr
sepehr / object_to_array.php
Created Aug 27, 2012
PHP: Recursively cast an object into array
View object_to_array.php
<?php
/**
* Recursively casts an object into array.
*
* @param $object
* Object to cast to array.
*
* @return array
*/
@sepehr
sepehr / rmdir_recursive.php
Last active Jul 9, 2018
PHP: Recursive directory removal
View rmdir_recursive.php
<?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 Aug 16, 2012
PHP: Mobile Browser Detection
View mobile_detect.php
<?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 Jun 12, 2018
OSX: Kong Installation
View osx-kong.sh
#!/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.
#
@sepehr
sepehr / blockstack.txt
Created Dec 20, 2017
Blockstack proof
View blockstack.txt
Verifying my Blockstack ID is secured with the address 19qPLyc5KHs1y6eBSB6JQTBPMTXuWVASce https://explorer.blockstack.org/address/19qPLyc5KHs1y6eBSB6JQTBPMTXuWVASce
@sepehr
sepehr / desc
Last active Sep 7, 2017
Shell: Man a single option
View desc
#!/bin/bash
# Print information about a single option or command
# http://svn.mikelward.com/svn/scripts/desc
# Example Usage:
# opt bash continue
# opt rsync -v
scriptname=desc
@sepehr
sepehr / num_endianness.php
Created Aug 1, 2017
Change endianness of a decimal number in PHP
View num_endianness.php
<?php
/**
* Change endianness of the given decimal number.
*
* @param int $num
*
* @return int
*/
function num_endianness($num) {