Archive for September, 2009

How an Engineer folds a T-Shirt

RSS cheese

If you get bored with your blog RSS icon or just want a new or unusual one, check out a free icon set RSS Cheese.

According to the author the set contains 4 high resolution RSS icons that come in four sizes: 512×512, 256×256, 128×128 and 64×64 as well as icons are completely free and can be used without any restrictions in any type of project inlcuding commercial projects.

RSS Cheese

PHP: Don’t repeat parameters using sprintf()

While missing a C# method String.Format() I’ve found a similar function in PHP, i.e. sprintf(). But since this function is slightly different from String.Format() it is not obvious how to avoid duplicating the same parameters.

For instance how to re-write an example below without passing $url and $title twice.

$link = sprintf('<a href="%s" title="%s">%s</a> (%s)</li>',
              $url, $title, $title, $url);

The answer is less obvious compare to C# but still pretty handy:

$link = sprintf('<a href="%1$s" title="%2$s">%2$s</a> (%1$s)',
              $url, $title);

In both cases echo $link give the same output like:

<a href="http://www.google.com/" title="Google">Google</a> (http://www.google.com/)

but the second way makes the code more elegant 🙂 .

PHP: json_encode before 5.2.0

Even though it is a lot of talks about PHP 5.3 there are still many production environments which run PHP 5.1.*. As well as sometimes development servers are updated earlier than production ones for testing purposes. It may cause that some new PHP features will not work on production because of older version there.

One of the most common examples is ‘missing’ json_encode() function which became native with PHP 5.2.0 onward. Below is possible workaround while your production has not upgraded yet.

<?php
if (!function_exists('json_encode'))
{
  function json_encode($a=false)
  {
    if (is_null($a)) return 'null';
    if ($a === false) return 'false';
    if ($a === true) return 'true';
    if (is_scalar($a))
    {
      if (is_float($a))
      {
        // Always use "." for floats.
        return floatval(str_replace(",", ".", strval($a)));
      }
 
      if (is_string($a))
      {
        static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
        return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
      }
      else
        return $a;
    }
    $isList = true;
    for ($i = 0, reset($a); $i < count($a); $i++, next($a))
    {
      if (key($a) !== $i)
      {
        $isList = false;
        break;
      }
    }
    $result = array();
    if ($isList)
    {
      foreach ($a as $v) $result[] = json_encode($v);
      return '[' . join(',', $result) . ']';
    }
    else
    {
      foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
      return '{' . join(',', $result) . '}';
    }
  }
}
?>

Update: the same code is now available through GitHub.