Code Snippets

Creating SEO Friendly URLs in PHP with url_slug()

I’m talking about slugs, SEO friendly URLs, clean URLs, permalinks, etc. You know, those nice-looking URLs that are easy to read (for humans and search engines). The keywords that show up in your URLs plan an important role in how well your site ranks in search results. That’s one reason why it’s common for blogging systems like WordPress to create permalinks from post titles. I’ve had a function in my toolbox for doing this sort of thing for a while now. While working on an article management system the other day, though, I decided to take another look at it […]

mb_str_replace — The Missing PHP Function

I usually try to build UTF-8 support into the sites and web applications I build. In PHP land that usually means using the multibyte string extension mbstring. A number of people have noticed, though, that there are some often used PHP string functions with no mbstring equivalent. One of those functions is str_replace(). So for anyone looking for a mb_str_replace() function the one below should serve you nicely. It should work in exactly the same manner as str_replace().

Binary Search for Javascript Arrays

If you need to search through a large array, or you search arrays frequently in your Javascript code, or if you do both, chances are a binary search will give you better performance than a linear search (read: for loop). One caveat, however, is that binary search algorithms only work on sorted arrays. Here is a binary search function I sometimes use in my code:

Recursive Find and Replace With grep and Perl

I thought it might be a nice idea to start posting useful little commands and bits of code every now and then–ones I’ve found to be particularly useful. So here’s the first one, recursive find and replace. A masterfully crafted regular expression paired with this command can save you hours of tedious work.