Here’s a quick and easy view helper for Zend Framework that will encode an email address. It will encode just an email address or return a whole mailto link. The encoding is basically the same as in the Smarty template engine.
Obviously there’s a lot of room for improvement; javascript encoding, representation as an image, and so on… but then it wouldn’t be quick an easy – it’d be slightly longer and just a little more complex.
Continue reading ‘Quick and easy email encoding view helper’
Here’s a little view helper to display a tag cloud. All you have to do is supply an array of tags, with the tag name being the index and how many times it’s used as the value, and the url you’d like the tags to go to.
Continue reading ‘Tag cloud view helper’
Do you have a database with foreign keys and just wish you could have something automatically create your ZF models from it? Well, today that was me. So as a little proof of concept, this is the code I came up with to do it for me…
But before we get to that, a few caveats:
- It’s just a proof of concept
- The output needs updating for proper reference names, etc.
- Outputs everything to screen in one go and doesn’t save the files.
However, it might be handy to someone, so I post it up for your comments.
Continue reading ‘Auto generating basic models for a Zend Framework app’
Here’s a small function that will allow you to force a file download.
/**
* Force a file download via HTTP.
*
* File is required to be on the same server and accessible via a path.
* If the file cannot be found or some other error occurs then a
* '204 No content' header is sent.
*
* @param string $path Path and file name
* @param string $name Name of file when saved on user's computer,
* null for basename from path
* @param string $type Content type header info (e.g., 'application/vnd.ms-excel')
* @return void
* @access public
*/
/* public static */ function download($path, $name = null, $type = 'binary/octet-stream')
{
if (headers_sent()) {
echo 'File download failure: HTTP headers have already been sent and cannot be changed.';
exit;
}
$path = realpath($path);
if ($path === false || !is_file($path) || !is_readable($path)) {
header('HTTP/1.0 204 No Content');
exit;
}
$name = (empty($name)) ? basename($path) : $name;
$size = filesize($path);
header('Expires: Mon, 20 May 1974 23:58:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Cache-Control: private');
header('Pragma: no-cache');
header("Content-Transfer-Encoding: binary");
header("Content-type: {$type}");
header("Content-length: {$size}");
header("Content-disposition: attachment; filename=\"{$name}\"");
readfile($path);
exit;
}
Very easy to use, too! Here are some examples of how you might call the function:
download('./myfile.txt');
download(__FILE__, 'a file for you.php');
download('/home/you/files/spreadsheet.xml', 'ssheet_' . date('Ymd'), 'application/vnd.ms-excel');
Here’s a simply view helper for the Zend Framework that can be used to display image tags. It checks to see if the image file exists and if not then it’ll use the data url scheme to output a very simple image that, ironically, says ‘NO IMG’ on it.
Please note, though, that I’ve only seen Firefox support this scheme, as wonderful as it is!
Continue reading ‘Simple image view helper for Zend Framework’
I’m sure you’ve seen the simple email address format validation function; they’re usually a simple regular expressing that just check the address portion (the user@example.org bit). That’s really only a bit of the validation that should be done. The RFC822 specs detail that the format of email addresses can be much larger, for example, it could be something like “Andrew Collington & Co.” <a.collington@example.org>, and, of course, the simple regex on that would fail. But even a check on the address format isn’t often enough… The user could enter a correctly formatted email address but simply have mis-spelled the address… they may accidentally type in user@yahooo.com, or user@hitmail.co.uk rather than hotmail.co.uk, and things like that. In which case you may want to check the MX and/or A record to see if its a valid domain. And whilst you’re doing that, why not check to see if it’s a commonly used email host that maybe they’ve typed in wrong?
So here is a class that will allow you to do all that in one easy method call:
Continue reading ‘Robust email address validator – with address suggestions!’
So there you are nicely tagging your project in Subversion, but for some reason you need to get a list of all the tags being used… That situation came up for me today. I thought it was going to be some really complex way of getting the tags, involving the use of hook scripts and the like. But it turns out that with some command line goodness it’s actually much more simple. Here’s how to do it:
svnlook tree --full-paths /home/path/to/svn/project | \
egrep -a '/?tags/.+' | \
sed -re 's!.*/?tags/([^/]*).*!\1!' | \
sort -u
Create a random thumbnail of a video file
Looking at sites like YouTube, you may think it’s quite hard to create a lot of different thumbnails from video files, and have them from random times within that file. But, no, it’s not! As this article shows, by using the very fabulous FFmpeg library, it’s actually a very short amount of code that’s required to create all those lovely random thumbnails.
Continue reading ‘Create a random thumbnail of a video file’
This simple function allows you to generate a random registration key in the format, ‘1224-54B1-7D35-5EF7′.
function registration_key()
{
return strtoupper(substr(chunk_split(sprintf('%03d%s',
rand(0,999), uniqid('')), 4, '-'), 0, -1));
}
You’ve probably seen this before, if not a thousand times, but here’s a simple function to check the format of a UK postcode.
function valid_uk_postcode($postcode)
{
return (preg_match('/^([A-PR-UWYZ][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {0,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/i', $postcode)) ?
true : false;
}
Recent Comments