function.split.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 207 行

HTML
207
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Split string into array by regular expression</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.eregi.html">eregi</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.spliti.html">spliti</a></div> <div class="up"><a href="ref.regex.html">POSIX Regex Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.split" class="refentry"> <div class="refnamediv">  <h1 class="refname">split</h1>  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">split</span> &mdash; <span class="dc-title">Split string into array by regular expression</span></p> </div>  <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">array</span> <span class="methodname"><b><b>split</b></b></span>    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$pattern</tt></span>   , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$string</tt></span>   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$limit</tt></span>  ] )</div>  <p class="para rdfs-comment">   Splits a <i><tt class="parameter">string</tt></i> into array by regular expression.  </p>  </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">pattern</tt></i></span>     <dd>      <p class="para">       Case sensitive regular expression.      </p>      <p class="para">       If you want to split on any of the characters which are considered       special by regular expressions, you&#039;ll need to escape them first. If       you think <b>split()</b> (or any other regex function, for       that matter) is doing something weird, please read the file       <var class="filename">regex.7</var>, included in the        <var class="filename">regex/</var> subdirectory of the PHP distribution. It&#039;s       in manpage format, so you&#039;ll want to do something along the lines of       <strong class="command">man /usr/local/src/regex/regex.7</strong> in order to read it.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">string</tt></i></span>     <dd>      <p class="para">       The input string.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">limit</tt></i></span>     <dd>      <p class="para">       If <i><tt class="parameter">limit</tt></i> is set, the returned array will       contain a maximum of <i><tt class="parameter">limit</tt></i> elements with the       last element containing the whole rest of       <i><tt class="parameter">string</tt></i>.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   Returns an array of strings, each of which is a substring of   <i><tt class="parameter">string</tt></i> formed by splitting it on boundaries formed   by the case-sensitive regular expression <i><tt class="parameter">pattern</tt></i>.  </p>  <p class="para">   If there are <span class="replaceable">n</span> occurrences of   <i><tt class="parameter">pattern</tt></i>, the returned array will contain   <i><span class="replaceable">n</span>+1</i> items. For example, if   there is no occurrence of <i><tt class="parameter">pattern</tt></i>, an array with   only one element will be returned. Of course, this is also true if   <i><tt class="parameter">string</tt></i> is empty. If an error occurs,   <b>split()</b> returns <b><tt>FALSE</tt></b>.  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 <b>split()</b> example</b></p>    <div class="example-contents"><p>     To split off the first four fields from a line from     <var class="filename">/etc/passwd</var>:    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pass</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$uid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$gid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$extra</span><span style="color: #007700">)&nbsp;=<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">split</span><span style="color: #007700">(</span><span style="color: #DD0000">":"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$passwd_line</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #2 <b>split()</b> example</b></p>    <div class="example-contents"><p>     To parse a date which may be delimited with slashes, dots, or hyphens:    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Delimiters&nbsp;may&nbsp;be&nbsp;slash,&nbsp;dot,&nbsp;or&nbsp;hyphen<br /></span><span style="color: #0000BB">$date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"04/30/1973"</span><span style="color: #007700">;<br />list(</span><span style="color: #0000BB">$month</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$day</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$year</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">split</span><span style="color: #007700">(</span><span style="color: #DD0000">'[/.-]'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Month:&nbsp;$month;&nbsp;Day:&nbsp;$day;&nbsp;Year:&nbsp;$year&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p> </div> <div class="refsect1 notes">  <h3 class="title">Notes</h3>  <div class="tip"><b class="tip">Tip</b>   <p class="para">    <a href="function.preg-split.html" class="function">preg_split()</a>, which uses a Perl-compatible regular    expression syntax, is often a faster alternative to    <b>split()</b>.  If you don&#039;t require the power of regular    expressions, it is faster to use <a href="function.explode.html" class="function">explode()</a>, which    doesn&#039;t incur the overhead of the regular expression engine.   </p>  </div>  <div class="tip"><b class="tip">Tip</b>   <p class="para">    For users looking for a way to emulate Perl&#039;s <strong class="command">@chars =    split(&#039;&#039;, $str)</strong> behaviour, please see the examples for    <a href="function.preg-split.html" class="function">preg_split()</a> or <a href="function.str-split.html" class="function">str_split()</a>.   </p>  </div> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.preg-split.html" class="function" rel="rdfs-seeAlso">preg_split()</a></li>    <li class="member"><a href="function.spliti.html" class="function" rel="rdfs-seeAlso">spliti()</a></li>    <li class="member"><a href="function.str-split.html" class="function" rel="rdfs-seeAlso">str_split()</a></li>    <li class="member"><a href="function.explode.html" class="function" rel="rdfs-seeAlso">explode()</a></li>    <li class="member"><a href="function.implode.html" class="function" rel="rdfs-seeAlso">implode()</a></li>    <li class="member"><a href="function.chunk-split.html" class="function" rel="rdfs-seeAlso">chunk_split()</a></li>    <li class="member"><a href="function.wordwrap.html" class="function" rel="rdfs-seeAlso">wordwrap()</a></li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.eregi.html">eregi</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.spliti.html">spliti</a></div> <div class="up"><a href="ref.regex.html">POSIX Regex Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?