PHP标点符号全角转为半角方法

WBOY
Release: 2016-06-20 13:04:24
Original
3350 people have browsed it

PHP标点符号全角转为半角方法

将中文的全角转为英文半角符号实际上就是所有的中文标点的第二个字节减去0X80(即128)所得的数字就是半角所得的数字了。使用php正则替换方法即可实现全角转为半角的效果,代码中/e模式表达的是:如果设定了此修正符,preg_replace() 在替换字符串中对逆向引用作正常的替换,将其作为 PHP 代码求值,并用其结果来替换所搜索的字符串。具体代码如下:

<?php   <br />$str = "0123ABCDFWS\",.?<>{}[]*&^%#@!~()+-|:;";   <br />echo "$str";   <br />echo "<br />";   <br />$str = preg_replace('/\xa3([\xa1-\xfe])/e', 'chr(ord(\1)-0x80)', $str);   <br />echo $str;   <br />?>
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template