如何使用php将字符从全角转为半角

市面上很多评论系统,但是因为某种原因公司没去使用,评论系统使用php开发.垃圾评论中各种符号,各种全角数字或者字符,为了便于过滤,这边使用如下php程序将字符从全角转为半角用户后续的过滤 PHP字符全角转半角字符
function make_semiangle($str)
{
$arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
'5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
'y' => 'y', 'z' => 'z',
'(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
'】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
'‘' => '[', '’' => ']', '{' => '{', '}' => '}', '《' => '<',
'》' => '>',
'%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',
':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',
';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',
'”' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"',
' ' => ' ','$'=>'$','@'=>'@','#'=>'#','^'=>'^','&'=>'&','*'=>'*',
'"'=>'"');

return strtr($str, $arr);
}
来个实例,将全角转为半角
$oldWords="!!qq343824789!";
$newWords=make_semiangle("!!qq343824789!");
echo "oldWords:".$oldWords;
echo "</br>newWords:".$newWords;
在浏览器中输入测试地址;http://test.ttlsa.com/html/1771.php.得到如下结果: oldWords:!!qq343824789! newWords:!!qq343824789! 如下是我代码截图: [caption id="attachment_1774" align="aligncenter" width="635"] php将字符从全角转到半角的结果截图[/caption] [caption id="attachment_1775" align="aligncenter" width="512"] php将字符从全角转到半角的结果截图[/caption] 转载请注明出处: 如何使用php将字符从全角转为半角 http://www.ttlsa.com/html/1771.html

你可能感兴趣的:(如何使用php将字符从全角转为半角)