note 63878 added to function.str-replace
| From: | php-general at lists dot php dot net | Date: | Mon, 03 Apr 2006 02:57:02 +0000 |
| Subject: | note 63878 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-107196@lists.php.net to get a copy of this message | ||
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 );
----
Server IP: 216.194.113.175
Probable Submitter: 69.248.209.166
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- http://master.php.net/note/edit/63878
Del: integrated -- http://master.php.net/note/delete/63878/integrated
Del: useless -- http://master.php.net/note/delete/63878/useless
Del: bad code -- http://master.php.net/note/delete/63878/bad+code
Del: spam -- http://master.php.net/note/delete/63878/spam
Del: non-english -- http://master.php.net/note/delete/63878/non-english
Del: in docs -- http://master.php.net/note/delete/63878/in+docs
Del: other reasons-- http://master.php.net/note/delete/63878
Reject -- http://master.php.net/note/reject/63878
Search -- http://master.php.net/manage/user-notes.php