[ View menu ]

Replacing whole words in strings in PHP

May 15, 2008

I was recently tasked with creating a simple dirty word filter in PHP that relies on our blacklist of about 600 words. The filter should simply replace the word with its character length in asterisks.

My first attempt just used str_replace, but I quickly realized that even dirty words embedded in clean words would get filtered. For example, words like “glass” would get filtered to “gl***”.

The solution? Regular expressions:

function str_replace_word($needle,$replacement,$haystack){
    $pattern = "/\b$needle\b/i";
    $haystack = preg_replace($pattern, $replacement, $haystack);
    return $haystack;
}

The pattern is pretty simple, but not being very familiar with regular expressions, I had to do a couple Google searches before I found what I needed.

Here’s a simple breakdown of the pattern string:

The pattern delimiter

The pattern string begins and ends with a forward slash / character. These two characters are called the pattern delimiter and are required by the preg_match function. They can actually be any non-alphanumeric and non-backslash character, so you can pick your pattern delimiters to help with readability.

$pattern = '/\bMatch me\b/';
$pattern = '@\bMatch me\b@';
$pattern = '#\bMatch me\b#';

If you don’t supply a pattern delimiter, PHP will spit out this warning:

Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in

The word boundary \b

The \b escape character acts as a word boundary. For a nice explanation of this metacharacter, take a look at Jan Goyvaerts’ excellent regular expression tutorial page.

The ‘i’ at the end

The ‘i’ at the end of the pattern string just tells preg_match that the search should be case insensitive.

Campfest 2007

August 16, 2007

In early August of 2007, a group of 6 dapper young men set about on a week long trek through the wilderness of northern California and southern Oregon. They spent 3 nights surrounded by bears in Crags Castle National Park near Mount Shasta and another 3 frigid nights in Crater Lake National Park. Barely surviving on pizza, steak and chili, the brave young men spent their last moments together bickering about the validity of Chrono Trigger as a real video game before succumbing to hip and knee pain and going home.

Mount Shasta
Crags Castle National Park and Mount Shasta

Crater Lake National Park
Crater Lake National Park

Blizzcon ‘07

COSPLAY GIRLS TEH HAWT

Blizzon might as well be called WoWCon, and since I don’t play WoW, I had no idea what the panels were about. I spent a good amount of time just walking around, taking pictures and following the cosplay girls.

Take a look at the gallery.

Chicago

Chicago!

San Francisco Roadtrip

June 5, 2007

San Francisco Roadtrip

How to fix moiré

April 10, 2007

http://www.dbphoto.net/techniques/

Canyon Park Hike

April 9, 2007

Canyon Park Hike - Monrovia, CA

Canyon Park in Monrovia, CA.

Rocky Peak Hike

April 7, 2007

Rocky Peak Hike

Starring Robert Redford, Meryl Streep and Tom Cruise.

The Moon

March 29, 2007

Moon animation

PHP Excel Export class

March 27, 2007

Following the code that I found here, I made a quick PHP4 class that creates a single sheet Excel file. The class has a download() function that will send the appropriate headers for a binary download.
Continue reading PHP Excel Export class

« Previous