So at one time or another most programmers out there have had to deal with embedding characters, encoding types, content types and some text just plain mismatching. It’s never fun because it usually means you have to make some major changes in your database or your end file. That was the case today when I was working with Tim on bringing some XML into flash. It started off fine, but when we realized the case of the Microsoft Smart Quote had made it’s way into the database, I felt like hurting someone quickly.
Long story short, after no luck in embedding, setting content types and tweaking the heckout of flash, I found this post on php.net that saved the day.
function all_ascii( $stringIn ){
$final = ”;
$search = array(chr(145),chr(146),chr(147),chr(148),chr(150),chr(151));
$replace = array("’","’",‘"’,‘"’,‘-’,‘-’);
$hold = str_replace($search[0],$replace[0],$stringIn);
$hold = str_replace($search[1],$replace[1],$hold);
$hold = str_replace($search[2],$replace[2],$hold);
$hold = str_replace($search[3],$replace[3],$hold);
$hold = str_replace($search[4],$replace[4],$hold);
$hold = str_replace($search[5],$replace[5],$hold);
if(!function_exists(‘str_split’)){
function str_split($string,$split_length=1){
$count = strlen($string);
if($split_length < 1){
return false;
} elseif($split_length > $count){
return array($string);
} else {
$num = (int)ceil($count/$split_length);
$ret = array();
for($i=0;$i< $num;$i++){
$ret[] = substr($string,$i*$split_length,$split_length);
}
return $ret;
}
}
}
$holdarr = str_split($hold);
foreach ($holdarr as $val) {
if (ord($val) < 128) $final .= $val;
}
return $final;
}
?>
Example usage:
Thank you anonymous friend, you are my hero.