Showing entries with tag "PHP".

Found 6 entries

PHP: Disable output buffering for the CLI
When writing a PHP application that's going to be run from the CLI often you will want to disable output buffering. With output buffering enabled you will not see any output until your terminal buffer (usually 1k or 4k) fills up . This is accomplished with the...
PHP: IPV6 reverse DNS entries
...an IPV6 reverse entry, you have to: fully expand the address, reverse it, and add a period between each character. For example: `2001:db8::60` reverses to `0.6.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2`. I wrote a simple PHP function to handle this for me: ```PHP function ipv6_ptr($ip_str) { $hex = unpack("H*hex", inet_pton($ip_str)); $str = strrev($hex['hex']); $p = str_split($str); $ret = join(".",$p); return $ret; } ```
PHP: is_exception()
I have an object in PHP that I need to check if it is an exception and act appropriately. Surprisingly PHP does ***not*** have an `is_exception()` function built in so I had to write my own: ``` function is_exception($obj,$strict = false) { if (!is_object($obj)) { return false; } // It's some type of exception if ($obj...
PHP: Relative path between two directories
I need to calculate the relative path between two directories such that: ```php $a = "/one/two/three/four/"; $b = "/one/two/"; ``` `$a` to `$b` has a relative path of `../../` and `$b` to `$a` has a relative path of `three/four/`. This function solves that problem very simply: ```php // Borrowed from...
PHP: flat_var_export()
I needed to log a nested array to a file. To do this I needed to convert the array to a **single** line. After some searching I found [improved_var_export()](http://php.net/manual/en/reserved.classes.php#113971) that converts a PHP array/object to a single line textual representation. I cleaned up some of the formatting and this is the result. ```PHP function flat_var_export($variable, $return = false) { if ($variable instanceof stdClass) { $result =...
PHP: Quote Word
...If you pass a string to this function it will return an array of the words, stripping any separating whitespace. If you pass true as the second parameter you will instead get a hash returning each word in a key/value pair. ```PHP function qw($str,$return_hash = false) { $str = trim($str); // Word characters are any printable char $words = str_word_count($str,1,"!\"#$%&'()*+,./0123456789-:;?@[\]^_`{|}~"); if ($return_hash) { $ret = array(); ...