如何在字符串中查找字符的出现次数

GM.*_*GM. 1 php

我在PHP中搜索一个函数来返回字符串中字符的出现次数.

输入那些参数"hello world",'o'将返回2

Rag*_*geZ 8

substr_count 是你的朋友

var_dump( substr_count("hello world", 'o') );
Run Code Online (Sandbox Code Playgroud)

注意:这也可以substr_count搜索子字符串

var_dump( substr_count("hello world", 'hello') );
Run Code Online (Sandbox Code Playgroud)