百科狗-知识改变命运!
--

substr_count() - 计算字串出现的次数 - php 字符串函数

梵高1年前 (2023-11-21)阅读数 14#技术干货
文章标签字符串

substr_count()

(PHP 4, PHP 5, PHP 7)

计算字串出现的次数

说明

substr_count(string $haystack, string $needle[,int $offset= 0[,int $length]]) : int

substr_count()返回子字符串$needle在字符串$haystack中出现的次数。注意$needle区分大小写。

Note:

该函数不会计算重叠字符串。参见下面的例子。

参数

$haystack

在此字符串中进行搜索。

$needle

要搜索的字符串。

$offset

开始计数的偏移位置。如果是负数,就从字符的末尾开始统计。

$length

指定偏移位置之后的最大搜索长度。如果偏移量加上这个长度的和大于$haystack的总长度,则打印警告信息。负数的长度 length 是从$haystack的末尾开始统计的。

返回值

该函数返回整型。

更新日志

版本说明
7.1.0开始支持负数的$offset和$length。
5.1.0新增$offset和$length参数。

范例

substr_count() - 计算字串出现的次数 - php 字符串函数

Example #1substr_count()范例

参见

  • count_chars()返回字符串所用字符的信息
  • strpos()查找字符串首次出现的位置
  • substr()返回字符串的子串
  • strstr()查找字符串的首次出现
It's worth noting this function is surprisingly fast. I first ran it against a ~500KB string on our web server. It found 6 occurrences of the needle I was looking for in 0.0000 seconds. Yes, it ran faster than microtime() could measure.
Looking to give it a challenge, I then ran it on a Mac laptop from 2010 against a 120.5MB string. For one test needle, it found 2385 occurrences in 0.0266 seconds. Another test needs found 290 occurrences in 0.114 seconds.
Long story short, if you're wondering whether this function is slowing down your script, the answer is probably not.
Making this case insensitive is easy for anyone who needs this. Simply convert the haystack and the needle to the same case (upper or lower).
substr_count(strtoupper($haystack), strtoupper($needle))
It was suggested to use
substr_count ( implode( $haystackArray ), $needle );
instead of the function described previously, however this has one flaw. For example this array:
array (
 0 => "mystringth",
 1 => "atislong"
);
If you are counting "that", the implode version will return 1, but the function previously described will return 0.
To account for the case that jrhodes has pointed out, we can change the line to:
substr_count ( implode( ',', $haystackArray ), $needle );
This way:
array (
 0 => "mystringth",
 1 => "atislong"
);
Becomes
mystringth,atislong
Which brings the count for $needle = "that" to 0 again.
a simple version for an array needle (multiply sub-strings):
Yet another reference to the "cgcgcgcgcgcgc" example posted by "chris at pecoraro dot net":
Your request can be fulfilled with the Perl compatible regular expressions and their lookahead and lookbehind features.
The example
 $number_of_full_pattern = preg_match_all('/(cgc)/', "cgcgcgcgcgcgcg", $chunks);
works like the substr_count function. The variable $number_of_full_pattern has the value 3, because the default behavior of Perl compatible regular expressions is to consume the characters of the string subject that were matched by the (sub)pattern. That is, the pointer will be moved to the end of the matched substring.
But we can use the lookahead feature that disables the moving of the pointer:
 $number_of_full_pattern = preg_match_all('/(cg(?=c))/', "cgcgcgcgcgcgcg", $chunks);
In this case the variable $number_of_full_pattern has the value 6.
Firstly a string "cg" will be matched and the pointer will be moved to the end of this string. Then the regular expression looks ahead whether a 'c' can be matched. Despite of the occurence of the character 'c' the pointer is not moved.
This will handle a string where it is unknown if comma or period are used as thousand or decimal separator. Only exception where this leads to a conflict is when there is only a single comma or period and 3 possible decimals (123.456 or 123,456). An optional parameter is passed to handle this case (assume thousands, assume decimal, decimal when period, decimal when comma). It assumes an input string in any of the formats listed below.
function toFloat($pString, $seperatorOnConflict="f")
{
  $decSeperator=".";
  $thSeperator="";
  $pString=str_replace(" ", $thSeperator, $pString);
  $firstPeriod=strpos($pString, ".");
  $firstComma=strpos($pString, ",");
  if($firstPeriod!==FALSE && $firstComma!==FALSE) {
    if($firstPeriod

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)