Posts in php

Python String Slices in PHP

This past weekend, I decided I wanted to bring one of my favourite shortcuts in Python to PHP strings: its slice notation. Limiting myself to userland code, there wasn’t much I could do to replicate the syntax. Implementing the ArrayAccess interface would allow me to overload array dimensions in a class, though I’d be stuck parsing string offsets for the necessary arguments.

My first attempt, writing only from how I remembered using the slice notation, got me half way there. It handled start and stop well in the case of positive steps, but failed to handle a variety of negative steps. It was a simple mistake, as I wasn’t handling all possible adjustments to the slice boundaries. After looking at CPython’s implementation, I was able to spot where I went wrong and hopefully achieve an implementation that mimics Python’s own behaviour. Here’s a small preview of its use:

written in php Read on →

Monitoring File Changes in PHP

I’ve been working on a small BDD test framework, and I found myself wanting to implement a --watch option. When the flag is set, the test runner would watch the current directory and re-run all specs when a change occurs. Though PHP offers the inotify extension, I wanted this option to be cross-platform and work without a PECL extension. So I decided to write my own implementation.

Monitoring directory changes can be done using stat() or fmtime() to get the last modification time for the directory. This includes files being renamed, added or deleted in a folder. By polling every second, for example, we can see whether or not such a change has been made to the directory tree.

written in php Read on →

Stringy 1.1.0 Released: A String Manipulation Library for PHP With Multibyte Support

Today I’ve tagged and released v.1.1.0 of Stringy, a string manipulation library for PHP 5.3+. It’s inspired by underscore.string.js as well as a few other libraries, and offers multibyte support for commonly used functions. For this release, I received a few pull requests, which resulted in the addition of new functionality. They included three new methods: isHexadecimal(), isSerialized() and isJson(). That makes a current total of 52 methods.

It can be found on GitHub, and easily installed using composer. I’m still welcoming pull requests as well as recommendations for new features, so all suggestions are appreciated!

written in php