note 63878 deleted from function.str-replace by felipe
| From: | felipe@php.net | Date: | Sun, 06 Jan 2008 12:48:31 +0000 |
| Subject: | note 63878 deleted from function.str-replace by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134877@lists.php.net to get a copy of this message | ||
Note Submitter:
----
Here's a function I put together from pieces other folks wrote. First, it replaces all MS Word
smart quotes, EM dashes and EN dashes with their ASCII equivalents, then it goes through and deletes
any non-ASCII characters (such as the 1/4 character). The function was written with
backwards-compatibility in mind, not performance. It worked successfully in PHP Version 4.2.3, but
not in Version 4.2.2. Hopefully it'll help out someone:
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: echo all_ascii( $myString );