mirror of
https://github.com/php/doc-zh.git
synced 2026-04-25 17:18:15 +02:00
190 lines
4.5 KiB
XML
190 lines
4.5 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: 27ae0a4a16cdfc868a884c0f0dad7023b5f2709c Maintainer: HonestQiao Status: ready -->
|
||
<!-- CREDITS: mowangjuanzi, Luffy -->
|
||
<!-- splitted from ./en/functions/strings.xml, last change in rev 1.2 -->
|
||
<refentry xml:id="function.trim" xmlns="http://docbook.org/ns/docbook">
|
||
<refnamediv>
|
||
<refname>trim</refname>
|
||
<refpurpose>去除字符串首尾处的空白字符(或者其他字符)</refpurpose>
|
||
</refnamediv>
|
||
|
||
<refsect1 role="description">
|
||
&reftitle.description;
|
||
<methodsynopsis>
|
||
<type>string</type><methodname>trim</methodname>
|
||
<methodparam><type>string</type><parameter>string</parameter></methodparam>
|
||
<methodparam choice="opt"><type>string</type><parameter>characters</parameter><initializer>" \n\r\t\v\x00"</initializer></methodparam>
|
||
</methodsynopsis>
|
||
<simpara>
|
||
此函数返回字符串 <parameter>string</parameter> 去除首尾空白字符后的结果。如果不指定第二个参数,<function>trim</function> 将去除这些字符:
|
||
</simpara>
|
||
&strings.stripped.characters;
|
||
</refsect1>
|
||
|
||
<refsect1 role="parameters">
|
||
&reftitle.parameters;
|
||
<variablelist>
|
||
<varlistentry>
|
||
<term><parameter>string</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
待处理的 <type>string</type>。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>characters</parameter></term>
|
||
<listitem>
|
||
&strings.parameter.characters.optional;
|
||
</listitem>
|
||
</varlistentry>
|
||
</variablelist>
|
||
</refsect1>
|
||
|
||
<refsect1 role="returnvalues">
|
||
&reftitle.returnvalues;
|
||
<simpara>
|
||
过滤后的字符串。
|
||
</simpara>
|
||
</refsect1>
|
||
|
||
<refsect1 role="examples">
|
||
&reftitle.examples;
|
||
<para>
|
||
<example>
|
||
<title><function>trim</function> 使用示例</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
$text = "\t\tThese are a few words :) ... ";
|
||
$binary = "\x09Example string\x0A";
|
||
$hello = "Hello World";
|
||
var_dump($text, $binary, $hello);
|
||
|
||
print "\n";
|
||
|
||
$trimmed = trim($text);
|
||
var_dump($trimmed);
|
||
|
||
$trimmed = trim($text, " \t.");
|
||
var_dump($trimmed);
|
||
|
||
$trimmed = trim($hello, "Hdle");
|
||
var_dump($trimmed);
|
||
|
||
// 清除 $binary 首位的 ASCII 控制字符
|
||
// (包括 0-31)
|
||
$clean = trim($binary, "\x00..\x1F");
|
||
var_dump($clean);
|
||
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
string(32) " These are a few words :) ... "
|
||
string(16) " Example string
|
||
"
|
||
string(11) "Hello World"
|
||
|
||
string(28) "These are a few words :) ..."
|
||
string(24) "These are a few words :)"
|
||
string(5) "o Wor"
|
||
string(14) "Example string"
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>使用 <function>trim</function> 清理数组值</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
function trim_value(&$value)
|
||
{
|
||
$value = trim($value);
|
||
}
|
||
|
||
$fruit = array('apple','banana ', ' cranberry ');
|
||
var_dump($fruit);
|
||
|
||
array_walk($fruit, 'trim_value');
|
||
var_dump($fruit);
|
||
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
array(3) {
|
||
[0]=>
|
||
string(5) "apple"
|
||
[1]=>
|
||
string(7) "banana "
|
||
[2]=>
|
||
string(11) " cranberry "
|
||
}
|
||
array(3) {
|
||
[0]=>
|
||
string(5) "apple"
|
||
[1]=>
|
||
string(6) "banana"
|
||
[2]=>
|
||
string(9) "cranberry"
|
||
}
|
||
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
</refsect1>
|
||
|
||
|
||
<refsect1 role="notes">
|
||
&reftitle.notes;
|
||
<note>
|
||
<title>可能的问题:删除中间字符</title>
|
||
<para>
|
||
因为 <function>trim</function>
|
||
从字符串的开始和末尾修剪字符。所以中间是否删除字符可能会造成混淆。<literal>trim('abc', 'bad')</literal>
|
||
移除了“a”和“b”,是因为修剪了“a”之后,“b”移动到了开头,所以也被修剪了。所以这就是为什么能正常工作而
|
||
<literal>trim('abc', 'b')</literal> 看起来没有的原因。
|
||
</para>
|
||
</note>
|
||
</refsect1>
|
||
|
||
<refsect1 role="seealso">
|
||
&reftitle.seealso;
|
||
<simplelist>
|
||
<member><function>ltrim</function></member>
|
||
<member><function>rtrim</function></member>
|
||
<member><function>str_replace</function></member>
|
||
</simplelist>
|
||
</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
|
||
-->
|