Sponsor:

PHP Function: remove special characters from post data  

Posted at 6:33 am in PHP Functions,Programming,Tutorials

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("ñ" ,"&#241;", $str);
$str = str_replace("ñ" ,"&#241;", $str);
$str = str_replace("Á","&#193;", $str);
$str = str_replace("á","&#225;", $str);
$str = str_replace("É","&#201;", $str);
$str = str_replace("é","&#233;", $str);

$str = str_replace("ú","&#250;", $str);

$str = str_replace("ù","&#249;", $str);
$str = str_replace("Í","&#205;", $str);
$str = str_replace("í","&#237;", $str);
$str = str_replace("Ó","&#211;", $str);
$str = str_replace("ó","&#243;", $str);
$str = str_replace("“","&#8220;", $str);

$str = str_replace("”","&#8221;", $str);

$str = str_replace("‘","&#8216;", $str);
$str = str_replace("’","&#8217;", $str);
$str = str_replace("—","&#8212;", $str);

$str = str_replace("–","&#8211;", $str);
$str = str_replace("™","&trade;", $str);
$str = str_replace("ü","&#252;", $str);
$str = str_replace("Ü","&#220;", $str);
$str = str_replace("Ê","&#202;", $str);
$str = str_replace("ê","&#238;", $str);
$str = str_replace("Ç","&#199;", $str);
$str = str_replace("ç","&#231;", $str);
$str = str_replace("È","&#200;", $str);
$str = str_replace("è","&#232;", $str);
$str = str_replace("•","&#149;" , $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(“•”,”&#149;” , $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

List of Special Characters

Written by Anthony Damasco on March 11th, 2010 | 3 comments

3 Responses to 'PHP Function: remove special characters from post data'

  1. 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.

    WeaponsTheyFear

    30 Mar 10 at 2:12 pm

  2. Want to post an example for any readers browsing my blog?

    Anthony Damasco

    5 Apr 10 at 4:18 am

  3. 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

    Dewey Dufrain

    7 Apr 10 at 5:23 pm

Leave a Reply


Facebook
Follow damasconet on Twitter