What are the php string conversion functions?
php转换字符串函数有:1、addcslashes函数;2、addslashes函数;3、bin2hex函数;4、chr函数;5、convert_uuencode函数等等。
推荐:《PHP视频教程》
PHP字符串转换函数
addcslashes:以C语言风格使用反斜杠转义给定字符串中属于给定列表中的字符,此函数接受两个参数,第一个是要进行转义的字符串,第二个是需要进行转义的字符列表,并返回转义后的字符串,即属于转义字符列表中的字符前都加上了反斜杠。如果转义字符列表中包含\n、\r等字符,将以C语言风格转换,而其它非字母数字且ASCII码低于32以及高于126的字符均转换成使用八进制表示。当定义转义列表时可以通过在两个字符中间加上两个点来表示范围,范围内的字符都会被转义,在使用此种方式时要清除定义的范围内是否都是想要转义的字符,如果设置范围的结束字符ASCII码低于开始字符,则会产生警告并且不会创建范围,而是将开始字符、结束字符及其中的所有字符逐个转义。
addslashes:使用反斜杠引用字符串,接收一个参数,要转义的字符串,返回转义后的字符串,转义的目的是为了数据库查询语句等需要在某些字符前加反斜杠,这些字符有单引号、双引号、反斜杠与NUL字符。
bin2hex:把字符串的二进制字符串转换为十六进制字符串,转换使用字节方式,高四位字节优先。等价于将单个字符做dechex(ord())。
chr:返回指定的字符,接收一个参数,返回对应于此参数的ascii码所指定的单个字符,与ord()是互补的。如果传入的值大于256将会返回对256取模后的数字对应的ascii码所指定的单个字符。
convert_cyr_string:将西里尔(Cyrillic)字符从一种字符集转换为另一种字符集,接受三个参数,要转换的字符串,原始的字符集类型,新的字符集类型,返回转换后的字符串。字符集类型为单个字符,k(koi8-r)、w(windows-1251)、i(iso8859-5)、a(x-cp866)、d(x-cp866)、m(x-mac-cyrillic)。
convert_uudecode:解码一个 uuencode 编码的字符串,接受一个uuencode编码的字符串,返回解码后的字符串,如果解码失败返回false。
convert_uuencode:使用uuencode算法对一个字符串进行编码,接受一个要进行编码的字符串,返回编码后的字符串,如果编码失败返回false。
hex2bin:将十六进制字符串转换为二进制字符串, 接受一个十六进制字符串,返回转换后的给定字符串的二进制表示的字符串。此方法不是将十六进制数字转换为二进制数字。与bin2hex互逆。
html_entity_decode:将HTML实体转换为适当的字符。接受三个参数,第一个为必需的要转换的字符串,第二个为可选的标记位,指定了如何处理引号和使用哪种文档类型,默认值是ENT_COMPAT|ENT_HTML401,第三个参数是可选的指定转换字符时使用的编码。如果省略,PHP5.6起,php.ini配置项default-charset的值为默认值,PHP5.4、5.5默认为UTF-8,再之前默认为ISO-8859-1。返回转换后的字符。
htmlentities:将字符转换为HTML转义字符。接受四个参数,第一个参数为必需的要转换的字符串,第二、第三个参数与html_entity_decode函数相同,第四个参数为可选的布尔类型值,如果为false,则不会转换现有的HTML实体,否则全部转换,默认为true,返回转换后的字符,如果要转换的字符串中包含指定编码中无效的单元序列,且没有设置ENT_IGNORE或ENT_SUBSTITUTE标记,则会返回空字符串。
htmlspecialchars_decode:将特殊的HTML实体转为普通字符,接受两个参数,第一个为必需的要转换的字符串,第二个为可选的标记位,指定了如何处理引号和使用哪种文档类型,默认值为ENT_COMPAT|ENT_HTML401。返回转换后的字符串。被转换的实体有&, " (没有设置ENT_NOQUOTES 时), ' (设置了 ENT_QUOTES 时), < 以及>。
htmlspecialchars:将特殊字符转换为HTML实体,接受四个参数,与htmlentities函数相同。
ord:返回字符串的ascii码值,接受一个要转换的字符串,返回字符串的ascii值。
quoted_printable_decode:将quoted-printable字符串转换成8bit字符串。
quoted_printable_encode:将8bit字符串转换成quoted-printable字符串。
str_rot13:对字符串执行ROT13转换,忽略非字母表中的字符。如果传入的是编码后的字符,则返回的会是原始字符。
stripcslashes:反引用一个使用addcslashes()转义的字符串。
quotemeta:转义元字符集,将. \ + * ? [ ^ ] ( $ )字符前加反斜杠。如果输入的字符串为空则返回false。
<?php echo addcslashes("zoo['.']", 'A..z')."\n"; echo stripcslashes("\z\o\o\['.'\]")."\n"; echo addcslashes("zoo['.']", 'z..A')."\n"; echo addslashes("what's this?")."\n"; echo addslashes("This is a NULL character: \x00")."\n"; echo bin2hex("Hello")."\n"; echo dechex(ord('H')).dechex(ord('e')).dechex(ord('l')).dechex(ord('l')).dechex(ord('o'))."\n"; echo chr(65)."\n"; echo chr(321)."\n"; echo convert_uuencode("hellophp"); echo convert_uudecode("(:&5L;&]P:'`` `")."\n"; echo hex2bin("48656c6c6f")."\n"; $orig = "\"hello\" <b>world</b>"; $a = htmlentities($orig); $b = html_entity_decode($a); echo $a."\n"; // "hello" <b>world</b> echo $b."\n"; // "hello" <b>world</b> $str = "\x8F!!!"; echo htmlentities($str, ENT_QUOTES, "UTF-8")."\n";//空字符串 echo htmlentities($str, ENT_QUOTES | ENT_IGNORE, "UTF-8")."\n";//!!! $str = "<p><hello>"world"</p>\n"; echo htmlspecialchars_decode($str);//<p><hello>"world"</p> echo htmlspecialchars_decode($str, ENT_NOQUOTES);//<p><hello>"world"</p> echo htmlspecialchars("<p'hello'>world</p>", ENT_QUOTES)."\n";//<p'hello'>world</p> echo ord("2")."\n"; echo str_rot13("hello,world!")."\n"; echo str_rot13("uryyb,jbeyq!")."$n"; = "HelloWorld!\n"; echo quotemeta("hello?")."\n"; ?>
The above is the detailed content of What are the php string conversion functions?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
