Send Text Messages with PHP mail()

While the word is getting out, a very handy feature of many mail servers is the ability to send text messages is still surprisingly not well known. One caveat is that the service provider of the recipient has to be known – not a problem for a lot of applications where the user can provide […]

CodeIgniter – hyphenated URLs

In rewriting an existing site, with many links out in the wild (via advertisements, press releases, etc.), I had to find a way to use hyphenated urls (in the position of controller, method or argument). Checking SO and the CI boards showed various takes. I’ll go through each, and end with my approach. 1. Using […]

PHP – remove empty elements from array using native functions

I’ve seen this question for years, and it’s easy enough to write it out depending on your specific needs, but a simple way to remove empty array elements (“empty” meaning any element that evaluates to false), using build-in functions, while updating indexes, is: array_values(array_filter($array)); array_filter – if passed with no callback, will automatically remove all […]

PHP on Windows and VC versions

For no apparent reason, PHP crapped out on me the other day (wouldn’t load ANY modules – the most obvious error was ‘undefined function mysql_connect’). I had PHP 5.3.5 VC6, as tells us to use this build when using PHP as an Apache module. So I go to re-install (the plan was just to […]

PHP – simple Element class

I’m working on an “advanced” version of an XHTML/HTML5 outputter class (with element-specific validation and error-reporting, structural hints, etc), that’s actually wildly different than what follows, but I’ve found myself coming back to this basic version a couple times (usually extending it for modularized page fragments), so thought I’d post it quickly. As usual, no […]

PHP vs JS conditionals and ternary

Just a quick note about how PHP conditional operators differs from ECMA (JS and AS)… The double-pipe ‘OR’ operator, in JS, evaluated to the value of the expression that is not false. In PHP, it evaluates to true if any any expression is true, or false if not (the output will actually be “1” if […]

PHP + RegExp: Adding line breaks to dynamic text

When outputting user- (or client-) supplied content from a DB, you’ll often have very long line runs. For readability, you can easily break those into bit-sized lines: preg_replace(‘/(.{80})/’, "$1\n", $str); The above will insert a line break into $str at every 80th character. I’ll update this to account for content containing markup at some point.

PHP – Simple File Loader Class

just a little static class with one static method to grab a file and return the (processed) content as a string. class Fragment { public static function load($file, $variables = NULL){ if(!file_exists($file) || !is_file($file)){ throw new Exception(‘File not found’); } if(is_object($variables)){ $variables = get_object_vars($variables); } if(is_array($variables)){ extract($variables); unset($variables); } ob_start(); include $file; $output = ob_get_contents(); […]

PHP – send include file to string

obviously file_get_contents is preferred if reading raw text, but to send the output of file include (with contained PHP parsed), output buffering is required. the upshot: function file_to_string($file){ ob_start(); include $file; $buffer = ob_get_contents(); @ob_end_clean(); return $buffer; } POC: // contents of print date(‘m/d/y’); function file_to_string($file){ ob_start(); include $file; $buffer = ob_get_contents(); @ob_end_clean(); return […]

PHP – controller should invoke URI segments only if method is public

In a lot of controller classes lately, people are using the first segment of the URI as the method, and testing with something like this: class Controller { public function __construct(){ // parse URI – we’ll pass the first segment to $method if(method_exists($this, $method){ call_user_func(array($this, $method)); } } } I wanted a way to only […]