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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools