How to Parse Query Strings in PHP
Here is a quick snippet for parsing query strings in PHP. You can use the `parse_url` and `parse_str` methods. `parse_str` will create a variable for you populated with results. $url = "http://www.seanbehan.com/path/?param=key"; parse_str(parse_url...
Written by Sean Behan on 12/04/2017
How to Cast an Array to an Object with PHP
It's short and sweet. $obj_from_array = (object)['key'=>'val']; echo $obj_from_array->key;
Written by Sean Behan on 12/02/2017
PHP Headers for Sending CSV File Downloads
If you would like to force a file download prompt to the user, instead of just outputting the text to the browser, use the following headers. ...
Written by Sean Behan on 11/25/2017
How to Calculate Age with PHP
Calculate age in PHP $your_birthday = '1982-08-29'; $difference = date_diff(date_create(), date_create($your_birthday)); $age = $difference->format('%Y'); // 35 Short and sweet
Written by Sean Behan on 11/11/2017
How to Get a Random Item from an Array in PHP
Use this snippet for grabbing a random item from an array in php $fruits = ['apple', 'banana', 'carrot', 'date', 'elderberry']; echo array_rand(array_flip($fruits)); // => 'banana' PHP's `array_flip` makes the keys the values and the valu...
Written by Sean Behan on 11/10/2017
How to Slugify a String in PHP
Here is a snippet for generating a URL friendly slug in PHP function slugify($string){ return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string), '-')); } And you can use it in your code like so ...
Written by Sean Behan on 10/26/2017
How to Generate a UUID in PHP 7
Here is a little snippet for generating a UUID in PHP 7 function uuid(){ $data = random_bytes(16); $data[6] = chr(ord($data[6]) & 0x0f | 0x40); $data[8] = chr(ord($data[8]) & 0x3f | 0x80); return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_...
Written by Sean Behan on 10/26/2017
Matching email addresses in Javascript
Matching email addresses in Javascript regex = /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/img "hello sean@example.com how are you? do you know bob@example.com?".match(regex) // => ["sean@example.com", "bob@example.com"]
Written by Sean Behan on 03/24/2017
Find a File On Your File System with the Unix Find Command By Extension
find path/ -name *.txt
Written by Sean Behan on 11/05/2013
A Couple of Usefule Snippets When Working with Textmate
Here is the snippet f path/to/directory 'search string' | open_in_mate This snippet (technically 2 snippets) will recursively scan the contents of files in a directory and then open all the files it finds in Textmate. It's useful on large projec...
Written by Sean Behan on 10/18/2013
How to Split a Large File Into Smaller Files on OSX or Unix
Use the split command. split -l 1000 name-of-file output/directory You can set the number of lines (the "-l" flag) to split on and also the location of where the split files should be saved. Note, the output directory must already exists....
Written by Sean Behan on 10/08/2013
How to Create a Date Time Snippet in Sublime Text 2 (Dynamic Signature with Time Stamp)
You'll have to create a new plugin. From the menu bar select Tools > New Plugin Copy the Python script from the signature.py file. Remember to replace your email address (it's example.com). Save the file, signature.py, is fine. Next open up Prefer...
Written by Sean Behan on 06/17/2012