search
HomeBackend DevelopmentPHP ProblemWhat 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.

What are the php string conversion functions?

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 时), c05976aa6e52ee83d569d940dfba473d。

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[&#39;.&#39;]", &#39;A..z&#39;)."\n";
echo stripcslashes("\z\o\o\[&#39;.&#39;\]")."\n";
echo addcslashes("zoo[&#39;.&#39;]", &#39;z..A&#39;)."\n";
echo addslashes("what&#39;s this?")."\n";
echo addslashes("This is a NULL character: \x00")."\n";
echo bin2hex("Hello")."\n";
echo dechex(ord(&#39;H&#39;)).dechex(ord(&#39;e&#39;)).dechex(ord(&#39;l&#39;)).dechex(ord(&#39;l&#39;)).dechex(ord(&#39;o&#39;))."\n";
echo chr(65)."\n";
echo chr(321)."\n";
echo convert_uuencode("hellophp");
echo convert_uudecode("(:&5L;&]P:&#39;``
`")."\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&#39;hello&#39;>world</p>", ENT_QUOTES)."\n";//<p&#039;hello&#039;>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!

Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools