使用PHP将特殊字符转换为ascii
PHP中有没有内置函数可以将特殊字符转换为ascii代码?
Is there any built-in function in PHP to convert special characters to it's ascii code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试这个函数:
第一个参数是字符串,第二个参数是起始索引(如果您只指定一个特殊字符,那么这将为零)。
Try this function:
The first parameter is the string, the second the starting index (if you specify only one special character, then this will be zero).
是的,
ord
函数请参阅ord 手册页< /a>
编辑:还有
chr
做相反的事情。Yes, the
ord
functionSee the ord manual page
EDIT: there's also
chr
to do the opposite.ord 函数 返回字符的 ASCII 值。
还有一个相反的 chr ,它接受 ASCII 数字并返回字符。
如果您尝试将字符从一种字符集转换为另一种字符集,则可以使用 iconv 库
There is the ord function which returns the ASCII value of a character.
There is also its converse chr which takes an ASCII number and returns the character.
If you are trying to convert characters from one character set to another, then you can use the iconv library
只要您使用简单的 ASCII(仅表示基本英文字母小写+大写、阿拉伯数字和基本英文标点符号)。一旦您使用了更多字符,字符编码就会发挥作用。
首先,您始终需要记住您正在使用的编码 - 有些字符甚至不存在于某些编码中(纯 ASCII 仅包含 127 个字符),有些存在于一种编码中但不存在于另一种编码中,等等。需要知道你正在使用什么编码。
其次,一些编码使用多字节字符(例如utf-8)——也就是说,一个字符存储为一个或多个字节。这些也没有 ASCII 代码 - 请参阅 Joel Spolsky 关于 Unicode 的文章了解更多详情。
The previous responses are correct, as long as you are using plain ASCII (which means only basic English alphabet lower+uppercase, Arabic numbers and basic English punctuation). Once you are using more than that, character encodings come into play.
First of all, you always need to keep in mind what encoding you're using - some characters don't even exist in some encodings (plain ASCII only contains 127 characters), some exist in one encoding but not another, etc. So you need to know what encoding you're using.
Second, some encodings use multi-byte characters (e.g. utf-8) - that is, one character is stored as one or more bytes. Those don't have an ASCII code, either - see e.g. Joel Spolsky's article on Unicode for more details.