How to Handle Uploading Really Large Files in PHP
PHP has some default limits that do not work out of the box when you want to work with uploading very large files. So there are a few configuration changes you have to make to the defaults that come with PHP. These are defined in you php.ini file. The...
Written by Sean Behan on 09/20/2018
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
Simple Way to Calculate the Odds of Something Happening
Here is a quick way to express a chance of something happening. This is an example in JavaScript that gives you a 1 in 3 chance of being true. [true, false, false].sort(function(){ return Math.random() >= 0.5 ? 1 : -1 })[0] Basically, we popu...
Written by Sean Behan on 11/27/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
XPath with HTML in PHP One Liner
Here is a one liner for using XPATH with HTML in PHP $doc = new DOMXPath(@DOMDocument::loadHTML(file_get_contents("https://www.reddit.com/r/PHP/"))); Now you can use XPATH to query the html.. foreach($doc->query("//a") as $el){ echo $el->no...
Written by Sean Behan on 11/14/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 Use Named Variables with Postgres and PHP PDO Driver
You can write reusable scripts with Postgres by taking advantage of named variables. A named variable starts with a `:` in your sql script. Here is an example select :a_number You can then use this statement with `psql` and the `--variable` fl...
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
Connect to Postgres on Heroku using DATABASE_URL Config Var with PHP and PDO
Unfortunately PHP's PDO constructor doesn't take a database connection url (in a format that Heroku makes available as a config var) as an argument. It has its own, rather odd syntax. However, it's easy enough to extract url parts with the `parse_url`...
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
Here is a little snippet for generating a UUID in PHP 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_sp...
Written by Sean Behan on 10/26/2017
Apache Rewrite Rule for Mapping to index.php in Sub Folders
Below is an example URL structure with rewrite rules using an Apache .htaccess file. GET /a/ => a/index.php GET /a/b/ => a/b/index.php GET /a/b/c/ => a/b/index.php This URL mapping will give you pretty URLs in PHP without needing t...
Written by Sean Behan on 10/20/2017
Regex for Extracting URLs in Plain Text
Here is a Regex for extracting URLs from text. However, these links will not already be hyperlinked or source attribtues from images or iframes. This example is in PHP. I was trying to format a Wordpress page to auto hyperlink but preserve embeded ima...
Written by Sean Behan on 04/14/2017
I Just Published My First PHP Package HTMLXPATH
I just wrote and published my first PHP package on Packagist.com. It's available on [packagist.org](https://packagist.org/packages/htmlxpath/htmlxpath) and the source code is on [github](https://github.com/seanbehan/htmlxpath). To install the package u...
Written by Sean Behan on 03/28/2017
How To Increase or Change the File Upload Size in the PHP ini file for Wordpress
You need to find which configuration ini file has been loaded by PHP. The easiest way to do this is to run the phpinfo() function from the command line. php -r "phpinfo(); " | grep -i "loaded configuration file" You should see something like...
Written by Sean Behan on 08/24/2013
Deploying Wordpress on Heroku
> This post is out of date - Heroku officially supports PHP now w/ buildpacks Heroku runs Apache version 2.2.22 PHP version 5.3.10 Deploying Wordpress to Heroku requires that you throw the Wordpress source into a Git repository. So download th...
Written by Sean Behan on 03/02/2017
How to Check if the Current Logged In User can Edit a Course in MOODLE Using the Has_capability Function
After an hour of fruitless searching through the docs and forums, I finally found an answer to my simple question, of course buried in the depths of the shark infested, murky water that is the Moodle code base . How do I check if a user is a course creat...
Written by Sean Behan on 06/17/2012
Moodle's Most Important Function Gets No Attention
Arguably the most important function in the Moodle API, is the create_course function. One would think... after all Moodle is an LMS. Courses are the bread and butter for course management platforms, right? Taking a look at the function in the course/lib....
Written by Sean Behan on 06/17/2012
My Review of Moodle 1.9 Extension Development
I wrote a review for Joseph Thibault's Moodle News on extension development for Moodle. The book is quite good and I think an essential resource for anyone wanting to develop in Moodle. The book focuses on plugin development, but it will also give you an ...
Written by Sean Behan on 06/17/2012
Why are PHP5 Namespaces Defined Using a Backslash?
Why are PHP namespaces defined using a backslash? It looks ugly. Unless of course, there is a good reason for the "\"? Does this namespaced code run more efficiently on Windows? Since namespaces are new in PHP5 why not take the opportunity to use them wh...
Written by Sean Behan on 06/17/2012
Install and Serve a Rails Application from PHP Subdirectory Using Apache, Phussion Passenger and Ruby Enterprise Edition
Here is how to install a Rails application out of a subdirectory (rather than as a subdomain) with the Apache web server(Apache2). In this example I'm going to use my own blog which is a Wordpress installation and serve a Rails application from the subdir...
Written by Sean Behan on 06/17/2012
Generate MySQL Datetime Type Using PHP Date() Function
If you want to insert a datetime that matches the default mysql datetime type format use this date('Y-m-d H:i:s');
Written by Sean Behan on 06/17/2012
PHP Paypal IPN Script
Simple script for posting a valid response back to Paypal service after a sale is completed using the Paypal Instant Checkout payment method. <?php // read the post from PayPal system and add 'cmd' $req = 'cmd=_notify-validate'; foreach ($_POST as $k...
Written by Sean Behan on 06/17/2012
Listing Files and Directories with PHP
Listing all files and directories using PHP 5. <?php $files = array(); $dir = dir("."); while(false!==($file=$dir->read())): if(($file{0}!=".") && ($file{0}!="~") && (substr($file, -3)!="LCK") && ($file!=basename($_SERVER["PHP_SELF"]))): ...
Written by Sean Behan on 06/17/2012
Highlight String in PHP
Function for highlighting text/strings in PHP. $content = file_get_contents("http://php.net/"); print highlight("PHP", $content); function highlight($match, $string){ return str_ireplace($match, "<span style='background:yellow'>$match</span&...
Written by Sean Behan on 06/17/2012
Absolutize Relative Links Using PHP and Preg_Replace_Callback
I was in the market for a simple php script to replace hrefs with their absolute paths from scraped web pages. I wrote one myself. I used the preg_replace_callback function so that I could pass the parsed results as a single variable. <?php $domain =...
Written by Sean Behan on 06/17/2012
TinyMCE Rich Text Editor: HELLO EDITOR Plugin Tutorial and Example
I wanted to create a button for the TinyMCE Rich Text Editor for Wordpress. It was tough to find good docs on the subject. There are a couple of useful posts out there but in general I found them lacking. http://codex.wordpress.org/TinyMCE_Custom_Buttons...
Written by Sean Behan on 06/17/2012
Ruby on Rails vs. PHP in a Nutshell
[youtube]http://www.youtube.com/watch?v=p5EIrSM8dCA[/youtube]
Written by Sean Behan on 06/17/2012