Home > Article > Backend Development > What are the php string conversion functions?
php string conversion functions include: 1. addcslashes function, which uses backslashes to escape characters belonging to a given list in a given string; 2. addslashes function, which uses backslashes to quote strings; 3 , bin2hex function, convert the binary string of the string into a hexadecimal string, etc.
Recommended: "PHP Video Tutorial"
PHP string conversion functions are:
addcslashes: Use backslashes to escape characters in the given string that belong to the given list in C language style. This function accepts two parameters, the first is the string to be escaped, and the second is a list of characters that need to be escaped, and the escaped string is returned, that is, characters belonging to the escaped character list are preceded by a backslash. If the escape character list contains characters such as \n and \r, they will be converted in C language style, while other non-alphanumeric characters with ASCII codes lower than 32 and higher than 126 will be converted to octal representation. When defining an escape list, you can express the range by adding two dots between two characters. The characters in the range will be escaped. When using this method, you need to clear whether you want to escape all the characters in the defined range. characters. If the ASCII code of the end character of the set range is lower than the start character, a warning will be generated and the range will not be created. Instead, the start character, the end character, and all characters in them will be escaped one by one.
addslashes: Use backslashes to quote strings, receive a parameter, the string to be escaped, and return the escaped string. The purpose of escaping is for database query statements You need to add a backslash before certain characters. These characters include single quotes, double quotes, backslashes and NUL characters.
bin2hex: Convert the binary string of the string into a hexadecimal string. The conversion uses byte mode, with the high nibble taking precedence. Equivalent to doing dechex(ord()) on a single character.
chr: Returns the specified character, receives a parameter, and returns a single character specified by the ascii code corresponding to this parameter. It is complementary to ord(). If the value passed in is greater than 256, a single character specified by the ASCII code corresponding to the number modulo 256 will be returned.
convert_cyr_string: Convert Cyrillic characters from one character set to another. It accepts three parameters, the string to be converted, and the original character set type. , the new character set type, returns the converted string. The character set type is a single character, k (koi8-r), w (windows-1251), i (iso8859-5), a (x-cp866), d (x-cp866), m (x-mac-cyrillic) .
convert_uudecode: Decode a uuencode-encoded string, accept a uuencode-encoded string, and return the decoded string. If the decoding fails, return false.
convert_uuencode: Use the uuencode algorithm to encode a string, accept a string to be encoded, and return the encoded string. If the encoding fails, return false.
hex2bin: Converts a hexadecimal string to a binary string, accepts a hexadecimal string, and returns the converted binary representation of the given string. . This method does not convert a hexadecimal number to a binary number. Reciprocal with bin2hex.
html_entity_decode: Convert HTML entities to appropriate characters. Accepts three parameters, the first is the required string to be converted, the second is the optional flag bit, specifies how to handle quotation marks and which document type to use, the default value is ENT_COMPAT|ENT_HTML401, the third parameter Optionally specifies the encoding to use when converting characters. If omitted, starting from PHP 5.6, the value of the php.ini configuration item default-charset is the default value. PHP 5.4 and 5.5 default to UTF-8, and the previous default is ISO-8859-1. Returns the converted character.
htmlentities: Convert characters to HTML escape characters. Accepts four parameters. The first parameter is the required string to be converted. The second and third parameters are the same as the html_entity_decode function. The fourth parameter is an optional Boolean type value. If it is false, it will not be converted. Existing HTML entities, otherwise all are converted, the default is true, and the converted characters are returned. If the string to be converted contains an invalid unit sequence in the specified encoding, and the ENT_IGNORE or ENT_SUBSTITUTE tag is not set, an empty string will be returned. .
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!