1

我正在寻找一种方法来查找另一个句子中的句子出现次数

例如(我有):

你好兄弟,我一直在等着告诉你一件很重要的事情。再见兄弟,下周见

我正在寻找:

兄弟,我

这应该向我展示以下结果:

结果 = 2

4

2 回答 2

2

使用substr_count()函数

$str = "Hello brother, I have been waiting to tell you something very important. Good bye brother, I will see you next week";

echo substr_count($str, 'brother, I'); // 2

如果您需要不区分大小写的方式,只需将两个字符串都设为小写或大写:

echo substr_count(strtolower($str), strtolower('brother, I')); // 2
于 2013-02-27T19:29:56.960 回答
1
$str = "Hello brother, I have been waiting to tell you something very important. Good bye brother, I will see you next week"

// explode the string using delimiter 'brother, I', the no of occurance of 'brother, I' will be one less than than the number of element in resultant array.
eg

$resArr = explode("brother, I","$str");

$ans = count($resArr) -1

//assuming your input string is not starting or ending with delimiter ie 'brother, I' in this case
于 2013-02-27T19:22:32.450 回答