PHP: Remove special chars and punctuation from strings

This function is useful for languages like Portuguese, french, etc..

Hope it helps someone out there

Here’s the code:

function normalizeString ($s = '') 
{
  $str = htmlentities($s);
  $str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
  $str = preg_replace("/[^A-Z0-9]/i", ' ', $str);
  $str = preg_replace("/\s+/i", ' ', $str);	
  $str = trim($str);
  return $str;
}

by Humberto

Leave a Reply

You must be logged in to post a comment.