1
0
mirror of https://github.com/php/doc-zh.git synced 2026-04-24 00:28:08 +02:00
Files
mowangjuanzi 6a05d32f1a Update strings
2025-06-30 23:26:12 +08:00

175 lines
4.8 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 45042fef652f1b4e904e809fcbfcf31f6c60670b Maintainer: daijie Status: ready -->
<!-- CREDITS: mowangjuanzi -->
<!-- splitted from ./en/functions/strings.xml, last change in rev 1.27 -->
<refentry xml:id="function.substr-count" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>substr_count</refname>
<refpurpose>计算字串出现的次数</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>int</type><methodname>substr_count</methodname>
<methodparam><type>string</type><parameter>haystack</parameter></methodparam>
<methodparam><type>string</type><parameter>needle</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>offset</parameter><initializer>0</initializer></methodparam>
<methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>length</parameter><initializer>&null;</initializer></methodparam>
</methodsynopsis>
<para>
<function>substr_count</function> 返回子字符串
<parameter>needle</parameter> 在字符串
<parameter>haystack</parameter> 中出现的次数。注意
<parameter>needle</parameter> 区分大小写。
</para>
<note>
<para>
该函数不会计算重叠字符串!参见下面的例子。
</para>
</note>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>haystack</parameter></term>
<listitem>
<para>
在此字符串中进行搜索。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>needle</parameter></term>
<listitem>
<para>
要搜索的字符串。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>offset</parameter></term>
<listitem>
<para>
开始计数的偏移位置。如果是负数,就从字符的末尾开始统计。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>length</parameter></term>
<listitem>
<para>
指定偏移位置之后的最大搜索长度。如果偏移量加上这个长度的和大于 <parameter>haystack</parameter> 的总长度,则打印警告信息。
负数的长度 length 是从 <parameter>haystack</parameter> 的末尾开始统计的。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
该函数返回 <type>int</type>
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.0.0</entry>
<entry>
<parameter>length</parameter> 可以为空(nullable)。
</entry>
</row>
<row>
<entry>7.1.0</entry>
<entry>
开始支持负数的 <parameter>offset</parameter><parameter>length</parameter>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>substr_count</function> 示例</title>
<programlisting role="php">
<![CDATA[
<?php
$text = 'This is a test';
echo strlen($text), PHP_EOL; // 14
echo substr_count($text, 'is'), PHP_EOL; // 2
// 字符串被简化为 's is a test',因此输出 1
echo substr_count($text, 'is', 3), PHP_EOL;
// 字符串被简化为 's i',所以输出 0
echo substr_count($text, 'is', 3, 3), PHP_EOL;
// 输出 1,因为该函数不计算重叠字符串
$text2 = 'gcdgcdgcd';
echo substr_count($text2, 'gcdgcd'), PHP_EOL;
// 因为 5+10 > 14,所以抛出异常
echo substr_count($text, 'is', 5, 10), PHP_EOL;
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>count_chars</function></member>
<member><function>strpos</function></member>
<member><function>substr</function></member>
<member><function>strstr</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
-->