Skip to content

Instantly share code, notes, and snippets.

@luizventurote
Created April 13, 2014 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizventurote/10567179 to your computer and use it in GitHub Desktop.
Save luizventurote/10567179 to your computer and use it in GitHub Desktop.
Sanitize String - PHP.
<?php
/**
* Sanitize String
*
* @param string $str string to be cleaned
* @return string new string without special characters
*/
function sanitizeString($str) {
$str = preg_replace('/[áàãâä]/ui', 'a', $str);
$str = preg_replace('/[éèêë]/ui', 'e', $str);
$str = preg_replace('/[íìîï]/ui', 'i', $str);
$str = preg_replace('/[óòõôö]/ui', 'o', $str);
$str = preg_replace('/[úùûü]/ui', 'u', $str);
$str = preg_replace('/[ç]/ui', 'c', $str);
$str = preg_replace('/[,(),;:|!"#$%&/=?~^><ªº-]/', '_', $str);
$str = preg_replace('/[^a-z0-9]/i', '_', $str);
$str = preg_replace('/_+/', '_', $str);
return $str;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment