Archive for the ‘php’ Category
PCRE and newlines (in PHP)
I just wasted far too much time trying to work out why a regex would work for my example test cases but not with real data. Turns out “.”, DOT_ALL, and newlines are more complicated than I realised. My problem essentially came down to not knowing that the imap messages I was working with used [...]
IP Address Range PHP Class
I’m not sure how useful this piece of code really is, but it gave me the chance to write some funky PHP code (have a look at the next() method). Example usage <?php // Single host: foreach(new IpRange(’10.10.10.10′) as $ip) { echo $ip . "\n" } // >>> 10.10.10.10 // All hosts on [...]
total mysql rows
Turns out our Mysql server at work is a little bigger than I thought: Databases 75 Tables 1,549 Rows 1,018,085,348 However over the last couple of months, we’ve only averaged 130 queries/second Hacked up PHP to gather stats: class MysqlCounter { public function __construct($host, $username, $password) { [...]
i hate working with dates
This little snippet gets all the days in a month, and groups them by week. <?php error_reporting(E_ALL); // Return an array of all the days in a month grouped by week number // (Sunday is considered to be the first day of the week) function weeksInMonth($month, $year) [...]
Extracing links from HTML using PHP
Many months ago there was a PHP competition to make the smallest script to extract all the links from a document. I’ve lost a link to the actual site, but the rules and conditions were set up expecting everyone to solve the problem with regular expressions. In my opinion relying on regular expressions to parse [...]
Simple type checking in PHP
<?php error_reporting(E_ALL | E_STRICT); /* Manual optional type checking for PHP functions Basic example: function log_error($line_number, $filename, $desc) { CheckFunctionArgs(‘integer’, ‘string’, ‘string’); [snip] } Object example: class LogObject {} [...]
