note 111036 deleted from function.ord by aharvey
| From: | aharvey@php.net | Date: | Wed, 09 Jan 2013 01:44:22 +0000 |
| Subject: | note 111036 deleted from function.ord by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192851@lists.php.net to get a copy of this message | ||
Note Submitter: oskar dot mothander at gmail dot com
----
I needed to transform some UTF-8 text to ASCII to avoid bots indexing certain material which gave
googling users false hope of finding stuff. However ord() doesn't support UTF-8 and ASCII
doesn't support all UTF-8 chars so I made a neat function that converts all chars to CP437
which is an extended version of ACII that has a wider (but not total) support for UTF-8 chars.
(I'm not sure if google indeed not understand this but past experience tells me so)
<?php
function ascii_encode($str) {
iconv("UTF-8", "CP437", $str);
$encoded = '';
for ($i=0; $i < strlen($str); $i++){
$chr = substr($str,$i, 1);
if (ord($chr) == ord('&')) {
while (ord($chr) != ord(';') && $i < strlen($str)) {
$chr = substr($str,$i, 1);
$i++;
$encoded .= $chr;
}
$i--;
}
else {
$encoded .= '&#'.ord($chr).';';
}
}
return $encoded;
}
?>