I ran into trouble using this function when I moved a script from a server with PHP5 to a server with only PHP4.
As the last two parameters were added with 5.1.0, I wrote a substitute function:
<?php
function substr_count5($str,$search,$offset,$len) {
return substr_count(substr($str,$offset,$len),$search);
}
?>
Use it exactly as substr_count() is used in PHP5. (This will work in PHP5 as well.)
substr_count
(PHP 4, PHP 5)
substr_count — Compte le nombre d'occurrences de segments dans une chaîne
Description
substr_count() retourne le nombre d'occurrences de needle dans la chaîne haystack . Notez que needle est sensible à la casse.
Note: Cette fonction ne compte pas les chaînes de caractères qui se recouvrent. Voyez l'exemple ci-dessous !
Liste de paramètres
- haystack
-
La chaîne de caractères pour rechercher à l'intérieur
- needle
-
La chaîne de caractères que l'on recherche
- offset
-
Le décalage où on commence à compter
- length
-
La taille maximale après le décalage spécifié pour rechercher la chaîne. La fonction émet une erreur si le décalage plus la taille est plus grand que la taille de haystack .
Valeurs de retour
Cette fonction retourne un entier.
Historique
| Version | Description |
|---|---|
| 5.1.0 | Ajout des paramètres offset et length |
Exemples
Exemple #1 Exemple avec substr_count()
<?php
$text = 'Ceci est un test';
echo strlen($text); // 16
echo substr_count($text, 'est'); // 2
// la chaîne de caractères est réduite à 'st un test', alors elle affiche 1
echo substr_count($text, 'est', 6);
// le texte est réduit à 'st u', alors elle affiche 0
echo substr_count($text, 'est', 6, 4);
// génère une erreur parce que 8+10 > 16
echo substr_count($text, 'est', 8, 10);
// affiche seulement 1, parce que elle ne compte pas les chaînes de caractères
// qui se recouvrent
$text2 = 'gcdgcdgcd';
echo substr_count($text2, 'gcdgcd');
?>
substr_count
24-Jul-2007 03:37
06-May-2007 01:07
a simple version for an array needle (multiply sub-strings):
<?php
function substr_count_array( $haystack, $needle ) {
$count = 0;
foreach ($needle as $substring) {
$count += substr_count( $haystack, $substring);
}
return $count;
}
?>
26-Oct-2006 04:07
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))
21-Dec-2003 10:27
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.
