/**
* Combine two urls.
*
* The urls can be either a string or url parts that consist of:
*
* scheme, host, port, user, pass, path, query, fragment
*
* If passed in as parts in an array, the query parameter can be either
* a string or an array of name/value key pairs. The query parameters
* will be added on to the ones from the original url. If you want to
* remove query parameters, or any other parts of the url, you need to
* pass the value in as null.
*
* Examples:
*
* urlMerge(
* '/tests/section/people?id=9405',
* array('query' => array('found' => true, 'id' => null))
* );
*
* urlMerge(
* 'http://www.example.com/',
* array('scheme' => 'https', 'query' => 'foo=bar&test=1'
* );
*
* urlMerge(
* array('path' => '/tests/item', 'query' => 'id=9405'),
* 'http://www.example.com'
* );
*
* @param string|array $original
* @param string|array $new
* @return string
*/
function urlMerge($original, $new)
{
if (is_string($original)) {
$original = parse_url($original);
}
if (is_string($new)) {
$new = parse_url($new);
}
$qs = null;
if (!empty($original['query']) && is_string($original['query'])) {
parse_str($original['query'], $original['query']);
}
if (!empty($new['query']) && is_string($new['query'])) {
parse_str($new['query'], $new['query']);
}
if (isset($original['query']) || isset($new['query'])) {
if (!isset($original['query'])) {
$qs = $new['query'];
} elseif (!isset($new['query'])) {
$qs = $original['query'];
} else {
$qs = array_merge($original['query'], $new['query']);
}
}
$result = array_merge($original, $new);
$result['query'] = $qs;
foreach ($result as $k => $v) {
if ($v === null) {
unset($result[$k]);
}
}
if (!empty($result['query'])) {
$result['query'] = http_build_query($result['query']);
}
return (isset($result['scheme']) ? "{$result['scheme']}://" : '')
. (isset($result['user']) ? $result['user']
. (isset($result['pass']) ? ":{$result['pass']}" : '').'@' : '')
. (isset($result['host']) ? $result['host'] : '')
. (isset($result['port']) ? ":{$result['port']}" : '')
. (isset($result['path']) ? $result['path'] : '')
. (!empty($result['query']) ? "?{$result['query']}" : '')
. (isset($result['fragment']) ? "#{$result['fragment']}" : '');
}
Archive for the 'PHP' Category
Quite often I find myself having an array of objects and needing to sort that array by one or more of the properties in the objects… Imagine, for example, getting a large result set from Zend_Db, or something similar, and ordering in the query just takes too long. Or perhaps you’re getting results from a web service and that service doesn’t return the results in the order you’d like to use. Have you ever found yourself in that situation, too? On looking at the usort documentation one day I came across a comment by someone called Will Shaver that did almost what I wanted. With a little adaptation for my own use (being able to change the sort order, for example), it has become one of my favourite functions to use for sorting.
/**
* Sort an array of objects.
*
* You can pass in one or more properties on which to sort. If a
* string is supplied as the sole property, or if you specify a
* property without a sort order then the sorting will be ascending.
*
* If the key of an array is an array, then it will sorted down to that
* level of node.
*
* Example usages:
*
* osort($items, 'size');
* osort($items, array('size', array('time' => SORT_DESC, 'user' => SORT_ASC));
* osort($items, array('size', array('user', 'forname'))
*
* @param array $array
* @param string|array $properties
*/
public static function osort(&$array, $properties)
{
if (is_string($properties)) {
$properties = array($properties => SORT_ASC);
}
uasort($array, function($a, $b) use ($properties) {
foreach($properties as $k => $v) {
if (is_int($k)) {
$k = $v;
$v = SORT_ASC;
}
$collapse = function($node, $props) {
if (is_array($props)) {
foreach ($props as $prop) {
$node = (!isset($node->$prop)) ? null : $node->$prop;
}
return $node;
} else {
return (!isset($node->$props)) ? null : $node->$props;
}
};
$aProp = $collapse($a, $k);
$bProp = $collapse($b, $k);
if ($aProp != $bProp) {
return ($v == SORT_ASC)
? strnatcasecmp($aProp, $bProp)
: strnatcasecmp($bProp, $aProp);
}
}
return 0;
});
}
Now a few cools things about the function:
- It uses anonymous/lambda functions (or closures, whatever your prefer to call them), and that’s just plain fun
- You can sort on more than one property and because the sorting is recursive, it’ll sort the second property within the confines of the first, the third within the confines of the second, and so on. Think sorting in SQL
- You can sort in ascending or descending order for any of the properties
- It retains key associations so you could use this on an associative array of objects
- If the parameter you want to sort on is an array itself then you can use any value (by specifying it’s key) in that array as the sorting value
Zend_View_Stream is used pretty much when ever you use Zend_View, and I’ve blogged about how handy it is before. But as it’s a class like any other, you can extend it to give added functionality. One such use is to add automatic escaping to your view variables when you want. So instead of doing:
<?php echo $this->escape($this->var); ?> <?= $this->escape($this->var); ?>
You could simply do:
<?=~ $this->var; ?>
That’s a lot simpler, isn’t it?
Continue reading ‘Extend Zend_View_Stream to easily escape view variables’

Usually I’m a total wallflower at conferences, gravitating to only the people I know. This time round I’m trying to change that and speak to people, ask speakers questions, and all that.
Right now, though, I’m enjoying dinner.
Going to be travelling to the PHPNW Conference today (it’s tomorrow, but I don’t fancy catching the stupidly early train to get there on time), but going over the schedule is a pain… There are just too many good talks! How can I possibly go see them all?! The 11:15 time slot is easy, that’ll be Rob Allen’s talk on ZF 2 – we use it so much at work now that it’d be crazy to not find out what’s coming and how this may affect what we’re currently doing. Same with the Michelangelo van Dam talk on unit testing with ZF. But the 12:15, 15:00 and 16:15 talks? I have no idea what to choose! Juozas Kaziukena’s Optimizing Zend Framework might be worth while, but then again, is it all about ZF1 and how much will be relevant for ZF2? The HipHop talk by Scott McVicar would be interesting. I can’t see it being deployed at my work, should still be a good talk… I’m liking the sound of the Database version control without pain by Harrie Verveer as well! And that still leaves me with two other time slots to decide on… Sheesh!
The agony of choice, eh?
You see QrCodes popping up every now and again on sites, in publications and the like. I think they can be a very handy way for people with cameras on their phones to get a url or other content on to their phone very easily. (I’m thinking more about those people without iPhones or full keyboards, of course!)
If you’ve never seen a QrCode before, it looks something like this:
Now how cool would it be to be able to generate that automatically for each page on your site and allow people to be able bookmark that site on their phone? Well, I think it’d be pretty cool! So I came up with a very simple ZF view helper to do it for me.
I have just started to use the Zend_Feed related components in earnest and am really liking the Zend_Feed_Writer (new to ZF 1.10.0). So what I wanted to do was created an RSS feed file is one didn’t exist and then keep updating that file as-and-when new items came in. Seems a really easy and simple thing to do, right? That, unfortunately, has not been my experience.
I have to say that to documentation seems quite lacking on the ZF site (for all the the Feed components, really, not just the Writer). Because of that, what follows may be idiotic and there really is an easy way. If so, I hope that you will post up a comment and let me know because I’d love to learn!
On with what I did…
Continue reading ‘Adding new items to RSS feed – it shouldn’t be this hard!’
The other day I had a new book sent to me called PHP Team Development, written by Samisa Abeysinghe and published by Packt Publishing. Unfortunately, it arrived at work when I was on holiday so I haven’t been able to have a look at it yet. :-/ However, I’m back today and have the book in my hands (well, not literally, of course, else typing would be much more difficult), so am looking forward to diving in to it.
Hopefully have a bit of a review posted up here some time soon!
I think we can all agree that URL shortening services are great and are very handy to tidy up those long and obnoxious links. However, a lot of the time people simply forget to use them, or often don’t know about them in the first place. I’ve noticed this in a blog system I wrote using Zend Framework. On one hand I love that people post messages, but on the other it annoys me that they may supply a link that is so long it breaks the formatting of the page, or looks just plain ugly.
So what are my options? I could train everyone who posts blogs on the system to use a url shortening service or I could manually tweak all the links myself. As solutions they are not very practical at all; I don’t have the time to change any/all links myself, and I certainly don’t have enough patience to train everyone! So an automatic way of doing things is needed, and the filtering in Zend Framework comes to the rescue!
Continue reading ‘Shorten urls automatically with a Zend Framework filter’
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’




Recent Comments