Wed, 13/04/2011 - 20:36 — horuskol
PHP function to generate a Version 4 GUID based on IETF RFC 4122: A Universally Unique IDentifier (UUID) URN Namespace
function generateGUID() { $guid = ''; $time_low = str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT) . str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT); $time_mid = str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT); $time_high_and_version = mt_rand(0, 255); $time_high_and_version = $time_high_and_version & hexdec('0f');
Mon, 14/12/2009 - 19:28 — horuskol
Okay, so automically generating a friendly URL for a blog post or forum post from their titles doesn't really take a lot - but this snippet might make it easier for you:
function make_slug($orig, $length = 0) { $slug = preg_replace('/[^a-zA-Z0-9]/', '-', $orig); $slug = preg_replace('/\-\-+/', '-', $slug); $slug = strtolower($slug); if ($length > 0) { // limits the length of the slug $slug = substr($slug, 0, $length); } if (strrpos($slug, '-') == strlen($slug) - 1) { // removes any ending - $slug = substr($slug, -1); }
Tue, 06/10/2009 - 19:53 — horuskol
I recently helped a poster at HTML Forums who wanted to be able to reformat floating point numbers in PHP.
The problem is that some of these numbers were in exponential format. There was also a requirement not to round a number like 0.004 to 0, although the 'precision' was to be constant for all uses of this function in the script.
8.8458e-119 = 8.84e-119 1.06542e-52 = 1.07e-52 2.68e-36 = 2.68e-36 2.91405e-35 = 2.92e-35 0.0190644 = 0.01 0.0205511 = 0.02 0.004 = 0.004
Sun, 06/09/2009 - 14:13 — horuskol
A fun little function that will produce the Roman Numeral representation for any number from 1-3999.
The reason for the limitation is because the Romans used barred letters for the number 5000 and larger, and the representation of 4000 would be MV, which is hard to represent in a simple ASCII string (maybe as an exercise to the reader, the function can be extended?).
function intToRoman($number) { if (!is_numeric($number)) { echo "Input is not a number\r\n\r\n"; return; }
Sun, 26/07/2009 - 19:00 — horuskol
I recently helped another developer looking for a way to only allow access to one site from an iframe on another. Ideally, this would be done with the HTTP_REFERRER header, but this isn't always set by the browser, and so would prevent too many people from seeing the content properly.
My solution was simply to use a time-based key that could be generated on the site containing the iframe and tested on the remote server before allowing access to the content.
Mon, 13/10/2008 - 22:39 — horuskol
Description
Returns the numeric position of either the first needle found in the haystack, or the first instance of a needle in the haystack.
Parameters
The string to search in
An array of strings to search in the haystack for
Mon, 13/10/2008 - 22:39 — horuskol
<?php include_once('astrpos.php'); // or however you want to include it $needles = array( 'apple', 'banana', 'pear', 'orange' ); $haystack = 'The fruiterer has some bananas and apples today. Unfortunately, there are no oranges, but the pears will be in tomorrow.'; // this will return the position of 'banana' - the first needle in the haystack echo astrpos($haystack, $needles) . '<br>'; // this will return the position of 'apple' - the first needle in the array of needles echo astrpos($haystack, $needles, 0, ASTR_NEEDLE_ORDER) . '<br>';
Mon, 13/10/2008 - 22:39 — horuskol
<?php /** * function astrstr() * * Function to find the first instance of any needle from an array of strings * within a string * * * @author Stuart Jones <stuart@random-tweak.co.uk> * @copyright Copyright 2008 Stuart Jones * @version 08.11.25 * @license <a href="http://www.gnu.org/licenses/gpl.html<br /> " title="http://www.gnu.org/licenses/gpl.html<br /> ">http://www.gnu.org/licenses/gpl.html<br /> </a> * * @param string $haystack * @param array<string> $needles * @param [int $offset] * @param [int $flags] */ // define constants for function flags if (!defined('ASTR_NEEDLE_ORDER')) { define('ASTR_NEEDLE_ORDER', 1); }
Mon, 13/10/2008 - 22:39 — horuskol
Description
Parameters
The string to search in
An array of strings to search in the haystack for