<?php
/**
* @author MoXie
* @MSN [email protected]
* @QQ 178544833
*/
$Mx_Str = 'sdlgjljgldsjfljgbljdfoeiflkdgnlfb';
//源字符串
$Mx_Str_Co = 'fl';
//需要搜寻的字符串.
$Mx_Coun = 0;
//出现次数
$Mx_Coun_Arr = array();
//备用计数数组
//----------------------------------
// 利用Php自带函数
echo substr_count($Mx_Str,$Mx_Str_Co)."<br/>";
//-----------------------------------------------------
// 用历遍 实现
// 细节化操作方便.
for($i=1;$i <= strlen($Mx_Str);$i++){
$Mx_Str_Sub = null;
$Mx_Str_Sub = $Mx_Str[$i];
for($i_c=2;$i_c <= strlen($Mx_Str_Co);$i_c++){
$Mx_Str_Sub .= $Mx_Str[$i+$i_c-1];
}
if ($Mx_Str_Sub == $Mx_Str_Co){
$Mx_Coun++;
}
}
echo $Mx_Coun."<br/>";//-----------------------------------------------------
// 用正则 实现
// 效率高些
preg_match_all ("/$Mx_Str_Co/is",
$Mx_Str, $Mx_Coun_Arr);
echo count($Mx_Coun_Arr[0])."<br/>";;
//-----------------------------------------------------
// 用explode 实现
// 效率…………很难保证.
$Mx_Coun_Arr = explode($Mx_Str_Co,$Mx_Str);
$Mx_Coun = (count($Mx_Coun_Arr)-1);
echo $Mx_Coun."<br/>";
//---------------------------------
//还有很多实现方式.看你还要干什么了.
// ……还有10分太少了点.:(
?>
/**
* @author MoXie
* @MSN [email protected]
* @QQ 178544833
*/
$Mx_Str = 'sdlgjljgldsjfljgbljdfoeiflkdgnlfb';
//源字符串
$Mx_Str_Co = 'fl';
//需要搜寻的字符串.
$Mx_Coun = 0;
//出现次数
$Mx_Coun_Arr = array();
//备用计数数组
//----------------------------------
// 利用Php自带函数
echo substr_count($Mx_Str,$Mx_Str_Co)."<br/>";
//-----------------------------------------------------
// 用历遍 实现
// 细节化操作方便.
for($i=1;$i <= strlen($Mx_Str);$i++){
$Mx_Str_Sub = null;
$Mx_Str_Sub = $Mx_Str[$i];
for($i_c=2;$i_c <= strlen($Mx_Str_Co);$i_c++){
$Mx_Str_Sub .= $Mx_Str[$i+$i_c-1];
}
if ($Mx_Str_Sub == $Mx_Str_Co){
$Mx_Coun++;
}
}
echo $Mx_Coun."<br/>";//-----------------------------------------------------
// 用正则 实现
// 效率高些
preg_match_all ("/$Mx_Str_Co/is",
$Mx_Str, $Mx_Coun_Arr);
echo count($Mx_Coun_Arr[0])."<br/>";;
//-----------------------------------------------------
// 用explode 实现
// 效率…………很难保证.
$Mx_Coun_Arr = explode($Mx_Str_Co,$Mx_Str);
$Mx_Coun = (count($Mx_Coun_Arr)-1);
echo $Mx_Coun."<br/>";
//---------------------------------
//还有很多实现方式.看你还要干什么了.
// ……还有10分太少了点.:(
?>
(PHP 4 )substr_count -- Count the number of substring occurrences
Description
int substr_count ( string haystack, string needle)
substr_count() returns the number of times the needle substring occurs in the haystack string. 例子 1. substr_count() example<?php
print substr_count("This is a test", "is"); // prints out 2
?>