Recently while developing I came across the need to remove all special characters and numbers from a string. Actually, if it wasn’t an ASCII character, I wanted to get rid of it! I needed this for a method I was working on to use for my Auction Alerts site that I have been working on.

I think the best way to go about handling this would be to use a regular expression. To be more specific, I chose to use ereg_replace. I know that this function has been depreciated in PHP v5.3, meh, it still works like a charm. J

Anyway, an example of the code I used was this:

$cleansed_string = ereg_replace("[^A-Za-z ]", "", $string );

Oops, I forgot to mention that I wanted to leave spaces in it for reasons I’ll leave to your imagination. Glorious right? One line of code to filter all that other junk out. Well, I wasn’t done there.

I also needed this method to take out a number of commonly used words. The goal was to really leave in the words that are truly unique to the string. I don’t care about the words like “the, we, am, I, etc…”

$filter_words = array("the","and","it","we") //there are a lot more words in this array than shown
$filtered_string = str_ireplace($filter_words, " ", $cleansed_string);

It’s really as simple as that. You’ll notice that I used str_ireplace. This is a case-insensitive string replace. “The” is the same to me as “the” so I wanted the function to replace all occurrences found for those.

When all is said and done, $filtered_string is the variable that I returned from the method. It doesn’t have any numbers or special characters in it. Not only that, but it doesn’t have the useless crap words in like “I”.

Remove All Numbers from a String

I didn’t need this, but I thought I would add how to do that from this post. It isn’t much different than removing all characters other than the ASCIIs. All you need to really do is change the regular expression a bit.

$cleansed_string = ereg_replace("[^0-9]", "", $string );

BAMM! Done! Now $cleansed_string contains only numbers from the string.

I hope this little post was helpful you some of you. Let me know if you have any better methods for filtering strings like this.