⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.substr-count.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Count the number of substring occurrences</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.substr-compare.html">substr_compare</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.substr-replace.html">substr_replace</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.substr-count" class="refentry"> <div class="refnamediv">  <h1 class="refname">substr_count</h1>  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">substr_count</span> &mdash; <span class="dc-title">Count the number of substring occurrences</span></p> </div> <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">int</span> <span class="methodname"><b><b>substr_count</b></b></span>    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$haystack</tt></span>   , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$needle</tt></span>   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$offset</tt></span>   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$length</tt></span>  ]] )</div>  <p class="para rdfs-comment">   <b>substr_count()</b> returns the number of times the   <i><tt class="parameter">needle</tt></i> substring occurs in the   <i><tt class="parameter">haystack</tt></i> string. Please note that   <i><tt class="parameter">needle</tt></i> is case sensitive.  </p>  <blockquote><p><b class="note">Note</b>:        This function doesn&#039;t count overlapped substrings. See the example below!   <br />  </p></blockquote> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">haystack</tt></i></span>     <dd>      <p class="para">       The string to search in      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">needle</tt></i></span>     <dd>      <p class="para">       The substring to search for      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">offset</tt></i></span>     <dd>      <p class="para">       The offset where to start counting      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">length</tt></i></span>     <dd>      <p class="para">       The maximum length after the specified offset to search for the       substring. It outputs a warning if the offset plus the length is       greater than the <i><tt class="parameter">haystack</tt></i> length.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   This functions returns an <a href="language.types.integer.html" class="type integer">integer</a>.  </p> </div> <div class="refsect1 changelog">  <h3 class="title">ChangeLog</h3>  <p class="para">   <table class="informaltable">    <colgroup>     <thead valign="middle">      <tr valign="middle">       <th colspan="1">Version</th>       <th colspan="1">Description</th>      </tr>     </thead>     <tbody valign="middle" class="tbody">      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">5.1.0</td>       <td colspan="1" rowspan="1" align="left">        Added the <i><tt class="parameter">offset</tt></i> and        the <i><tt class="parameter">length</tt></i> parameters       </td>      </tr>     </tbody>    </colgroup>   </table>  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 A <b>substr_count()</b> example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'This&nbsp;is&nbsp;a&nbsp;test'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;14<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'is'</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;2<br /><br />//&nbsp;the&nbsp;string&nbsp;is&nbsp;reduced&nbsp;to&nbsp;'s&nbsp;is&nbsp;a&nbsp;test',&nbsp;so&nbsp;it&nbsp;prints&nbsp;1<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'is'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;text&nbsp;is&nbsp;reduced&nbsp;to&nbsp;'s&nbsp;i',&nbsp;so&nbsp;it&nbsp;prints&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'is'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;generates&nbsp;a&nbsp;warning&nbsp;because&nbsp;5+10&nbsp;&gt;&nbsp;14<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'is'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /><br /></span><span style="color: #FF8000">//&nbsp;prints&nbsp;only&nbsp;1,&nbsp;because&nbsp;it&nbsp;doesn't&nbsp;count&nbsp;overlapped&nbsp;subtrings<br /></span><span style="color: #0000BB">$text2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'gcdgcdgcd'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'gcdgcd'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.count-chars.html" class="function" rel="rdfs-seeAlso">count_chars()</a></li>    <li class="member"><a href="function.strpos.html" class="function" rel="rdfs-seeAlso">strpos()</a></li>    <li class="member"><a href="function.substr.html" class="function" rel="rdfs-seeAlso">substr()</a></li>    <li class="member"><a href="function.strstr.html" class="function" rel="rdfs-seeAlso">strstr()</a></li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.substr-compare.html">substr_compare</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.substr-replace.html">substr_replace</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

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