mirror of
https://github.com/php/doc-zh.git
synced 2026-04-28 02:33:12 +02:00
147 lines
4.6 KiB
XML
147 lines
4.6 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: 45042fef652f1b4e904e809fcbfcf31f6c60670b Maintainer: nio Status: ready -->
|
||
<!-- CREDITS: mowangjuanzi -->
|
||
<refentry xml:id="function.addcslashes" xmlns="http://docbook.org/ns/docbook">
|
||
<refnamediv>
|
||
<refname>addcslashes</refname>
|
||
<refpurpose>以 C 语言风格使用反斜线转义字符串中的字符</refpurpose>
|
||
</refnamediv>
|
||
|
||
<refsect1 role="description">
|
||
&reftitle.description;
|
||
<methodsynopsis>
|
||
<type>string</type><methodname>addcslashes</methodname>
|
||
<methodparam><type>string</type><parameter>string</parameter></methodparam>
|
||
<methodparam><type>string</type><parameter>characters</parameter></methodparam>
|
||
</methodsynopsis>
|
||
<para>
|
||
返回字符串,该字符串在属于参数 <parameter>characters</parameter>
|
||
列表中的字符前都加上了反斜线。
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="parameters">
|
||
&reftitle.parameters;
|
||
<para>
|
||
<variablelist>
|
||
<varlistentry>
|
||
<term><parameter>string</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
要转义的字符。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>characters</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
如果 <parameter>characters</parameter> 中包含有 <literal>\n</literal>,<literal>\r</literal>
|
||
等字符,将以 C 语言风格转换,而其它非字母数字且 ASCII 码低于 32 以及高于 126
|
||
的字符均转换成使用八进制表示。
|
||
</para>
|
||
<para>
|
||
当定义 <parameter>characters</parameter> 参数中的字符序列时,需要确保知道设置为开始及结束范围之内的字符都是什么。
|
||
<example>
|
||
<title>带范围的 <function>addcslashes</function></title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
echo addcslashes('foo[ ]', 'A..z');
|
||
// 输出:\f\o\o\[ \]
|
||
// 所有大小写字母均被转义
|
||
// ... 但 [\]^_` 以及分隔符、换行符、回车符等也一并被转义了。
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
另外,如果设置范围中的结束字符 ASCII
|
||
码高于开始字符,则不会创建范围,只是将开始字符、结束字符以及其间的字符逐个转义。可使用
|
||
<function>ord</function> 函数获取字符的 ASCII 码值。
|
||
<example>
|
||
<title><function>addcslashes</function> 中的字符顺序错误</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
echo addcslashes("zoo['.']", 'z..A');
|
||
// 输出:\zoo['\.']
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
当选择对字符 0,a,b,f,n,r,t 和 v 进行转义时需要小心。它们将被转换成
|
||
\0,\a,\b,\f,\n,\r,\t 和 \v,这些在 C 中都是预定义的转义序列。其中一些序列也在其它
|
||
C 派生语言(包含 PHP)中定义,这意味着如果使用 <function>addcslashes</function>
|
||
和 <parameter>characters</parameter> 中定义的字符输出生成语言的代码,将不会得到预期的结果。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
</variablelist>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="returnvalues">
|
||
&reftitle.returnvalues;
|
||
<para>
|
||
返回转义后的字符。
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="examples">
|
||
&reftitle.examples;
|
||
<para>
|
||
<parameter>characters</parameter> 参数,如“\0..\37”,将转义所有
|
||
ASCII 码介于 0 和 31 之间的字符。
|
||
<example>
|
||
<title><function>addcslashes</function> 例子</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$not_escaped = "PHP isThirty\nYears Old!\tYay to the Elephant!\n";
|
||
$escaped = addcslashes($not_escaped, "\0..\37!@\177..\377");
|
||
echo $escaped;
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="seealso">
|
||
&reftitle.seealso;
|
||
<para>
|
||
<simplelist>
|
||
<member><function>stripcslashes</function></member>
|
||
<member><function>stripslashes</function></member>
|
||
<member><function>addslashes</function></member>
|
||
<member><function>htmlspecialchars</function></member>
|
||
<member><function>quotemeta</function></member>
|
||
</simplelist>
|
||
</para>
|
||
</refsect1>
|
||
|
||
</refentry>
|
||
<!-- Keep this comment at the end of the file
|
||
Local variables:
|
||
mode: sgml
|
||
sgml-omittag:t
|
||
sgml-shorttag:t
|
||
sgml-minimize-attributes:nil
|
||
sgml-always-quote-attributes:t
|
||
sgml-indent-step:1
|
||
sgml-indent-data:t
|
||
indent-tabs-mode:nil
|
||
sgml-parent-document:nil
|
||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||
sgml-exposed-tags:nil
|
||
sgml-local-catalogs:nil
|
||
sgml-local-ecat-files:nil
|
||
End:
|
||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
vim: et tw=78 syn=sgml
|
||
vi: ts=1 sw=1
|
||
-->
|