PHP Function: remove special characters from post data
Since I began programming 6 years ago I always had trouble with collecting data from forms. Users tend to copy and paste data into them, capturing special characters in the process. The special chars then end up in the mysql data as some random garbage, then it appears on the website as weird symbols.
A while back I asked a programmer friend of mine if he had a function that would help out with this. He did! I’ve been using it with the last 9 applications that I built, and it works every time.
Here is the function code:
if (!function_exists('cleanText')) {
function cleanText($str){
$str = str_replace("Ñ" ,"Ñ", $str);
//$str = preg_replace('/Ñ/g',"|Ñ|", $str);
//echo "Text BEGIN ".$str." --- ".bin2hex ("Ñ")."\n<BR>"; // d1
/*
for($i = 0 ; $i < strlen($str) ; $i++){
echo "".$str{$i}." - ". bin2hex ( $str{$i})."<BR>";
}
*/
$str = str_replace("ñ" ,"ñ", $str);
$str = str_replace("ñ" ,"ñ", $str);
$str = str_replace("Á","Á", $str);
$str = str_replace("á","á", $str);
$str = str_replace("É","É", $str);
$str = str_replace("é","é", $str);
$str = str_replace("ú","ú", $str);
$str = str_replace("ù","ù", $str);
$str = str_replace("Í","Í", $str);
$str = str_replace("í","í", $str);
$str = str_replace("Ó","Ó", $str);
$str = str_replace("ó","ó", $str);
$str = str_replace("“","“", $str);
$str = str_replace("”","”", $str);
$str = str_replace("‘","‘", $str);
$str = str_replace("’","’", $str);
$str = str_replace("—","—", $str);
$str = str_replace("–","–", $str);
$str = str_replace("™","™", $str);
$str = str_replace("ü","ü", $str);
$str = str_replace("Ü","Ü", $str);
$str = str_replace("Ê","Ê", $str);
$str = str_replace("ê","î", $str);
$str = str_replace("Ç","Ç", $str);
$str = str_replace("ç","ç", $str);
$str = str_replace("È","È", $str);
$str = str_replace("è","è", $str);
$str = str_replace("•","•" , $str);
return $str;
}
}
?>
Stick this in code in a page called “functions.php” and include it at the top of the page. It uses str_replace to replace special characters. You can add and remove characters easily by coping $str = str_replace(“•”,”•” , $str); and just replacing the character and the code when necessary.
Here’s a snip of it in action
HMTL Form:
<form method="post" action="clean_data.php"> <input type="text" name="textfield"> <input name="submit" type="submit" /> </form> clean_data.php: <?php include "includes/functions.php"; // Get post Data from form $textfield = $_POST["textfield"]; // Clean Data $textfield= cleanText($textfield); echo $textfield; ?>
And that’s all there is to it. If you have any questions, post them in the comments of this post.
-AD
3 Responses to 'PHP Function: remove special characters from post data'
-
Want to post an example for any readers browsing my blog?
-
I’ve been reading through your site. You have some awesome posts on here, especially this one – I really liked it…nice post. Consider yourself bookmarked




Rather than calling so many str_replace functions, you could put the characters to replace in an array and the chars to replace with in an array and run one call to str_replace to do this.