Strip non-alphanumerical characters with PHP

Published: 13 years ago

In the past, when I first started using PHP in order to filter out bad characters for file uploads, seo friendly URL names and other things I created an array of bad characters.

This is obviously not an ideal situation as the amount of characters that people or scripts can inject is ridiculously high, so adding a new element in the array for this becomes unpractical.

Old Way

// manually enter each bad character
// Text to clean
$text = "This is @ 'sentence' with characters in it!!! "; 

// wrong wrong wrong!!
$bad_chars = array('/', " ", "!", "%", "&", "]", "[", "*", "<", ">",";", "`", "¬", "$", "£", "^");

// Start Cleaning
$text = str_replace($bad_chars,'',$text);

// print text
echo $text;

A more powerful and easy way to do this would be with regular expressions and filter just alpha-numerical characters. The example below shows how to do it with and without spaces.

  • 0-9
  • A-Z

Improved Way

// Text to clean
$text = "This is @ '''sentence''' _without_ characters in it!!! "; 

// Start Cleaning - Letters Only
$text = ereg_replace("[^A-Za-z]", "", $text);

// Start Cleaning - Without Spaces 
$text = preg_replace("/[^A-Za-z0-9]/", "", $text );

// Start Cleaning - With Spaces
$text = preg_replace("/[^a-zA-Z0-9s]/", "", $text );

// Show results
echo $text; // This is sentence without characters in it

comments powered by Disqus
:?>