Partial string replacement with fixed length in PHP

Today I came across one of those problems that look simple and straight forward at first, but then somehow consume a good part of an hour to solve.  I wanted to replace a part of the string (in the middle), with given character, without changing the length of the string itself.  Once scenario would be a credit card number, where I would want to replace all digits with an ‘x’, leaving only the first digit and the last four as they were.  I tried a number of approaches, including sscanf() and array_splice() ones.  My final version is below.  It might be somewhat longer than I expected, but I like the simplicity of it.

<?php
/**
 * Replace middle of the string with given char, keeping length
 *
 * <code>
 * print padString('1234567890123456', 1, 4, 'x');
 * </code>
 *
 * @param string $string String to process
 * @param integer $start Characters to skip from start
 * @param integer $end Characters to leave at the end
 * @param string $char Character to replace with
 * @return string
 */
function padString($string, $start, $end, $char) {
    $result = '';

    $length = strlen($string) - $start - $end;
    if ($length <= 0) {
        $result = $string;
    }
    else {
        $replacement = sprintf("%'{$char}" . $length . "s", $char);
        $result = substr_replace($string, $replacement, $start, $length);
    }

    return $result;
}
?>

P.S.: Chris posted an alternative solution. Please have a look.

CakePHP GraphViz Models

I have completely and totally rewritten my old script that generates a graph of CakePHP models and their relationships.  Instead of pasting the code in here, I pushed all of its development to GitHub where it now enjoys a new repository.  Please have a look, try it out, and let me know if it does or doesn’t work for you.

The major changes in this version are:

  • Rewritten as CakePHP Shell instead of being a standalone madness script.
  • Got rid of all dot markup. Utilized Image_GraphViz PEAR package instead.
  • Added support for old and new CakePHP versions (1.2, 1.3, and 2.0).
  • Added option for using only real models (via className property of the relationship) or sticking with the old behavior.
  • Added a bunch of options for tweaking GraphViz output.  And now it’s obvious where to edit them, in case you need more.
  • Improved the styling of the graph a bit – fonts and colors.
  • Improved documentation.

As a side effect improvement, now that it is a native CakePHP Shell, it’s trivial to add to your project build process.

Upgrading to PHP 5.2.x on CentOS

Today while setting up yet another project on my hosting server.  The server runs CentOS 5.6, which means PHP 5.1.6 is used.  However the new project required PHP 5.2.0+.  It turned out upgrading PHP is trivial.  There is even a Howto Guide in CentOS wiki.  The steps are:

  1. Add CentOS Testing repository to yum.
  2. yum update PHP packages.

That’s all folks!

Command line PHP

I’ve discovered two things about command line PHP today.  I’ll share them here just in case you missed them too.

First, the “-f” parameter is optional.  I’m not sure when the change happened, but I’m pretty sure back when I started using it, it was required.  Now, instead of “php -f some.php” you can run just “php some.php”.

Second, there is now an interactive PHP mode! This is something I wanted for years.  Prior to PHP I had experience with Perl and Python, both of which have interactive modes.  With PHP I had to resort to a gadzillion of tiny files with snippets of code.  Now I don’t have to anymore.  Just run “php -a” and type away.

Have a look at PHP command line options manual page.  Maybe you’ll discover something else.

Debugging with git bisect

Via Sebastian Bergmann’s blog I’ve learned about git bisect and how it can used for debugging.  Sebastian demonstrates the functionality together with PHPUnit.  I am a git newbie, so that was quite interesting for me.

git bisect can be used to find the change that introduced a bug. It does so by performing a binary search on the list of commits between a known good and a known bad state of the repository. A tool such as PHPUnit can be invoked at each step of the binary search to check whether or not the current state is broken.

Here is a shortcut on how to actually use it:

$ git bisect start
$ git bisect good someVersion
$ git bisect bad someOtherVersion
$ git bisect run someCommand -with SomeArgument

There are, of course, more resources online covering the feature.  I found this section of the Git Community Book helpful.  Hopefully, I’ll remember about it when I actually need it.