substr_count
(PHP 4, PHP 5)
substr_count — Conta o número de ocorrências de uma substring
Descrição
int substr_count
( string $haystack
, string $needle
[, int $offset
[, int $length
]] )
substr_count() retorna o número de vezes que a substring needle ocorre na string. Note que needle faz distinção de maiúscula e minúscula.
Nota: Esta função não conta substrings sobrepostas. Veja o exemplo abaixo!
Parâmetros
- haystack
-
A string onde será feita a busca
- needle
-
A substring a ser procurada
- offset
-
O Ãndice onde inicia a contagem
- length
-
O valor máximo depois do Ãndice especificado para buscar pela substring. Mostra um aviso se o tamanho do Ãndice for maior que o tamanho de haystack .
Valor Retornado
Esta função retorna um integer.
Histórico
Versão | Descrição |
---|---|
5.1.0 | Adiciona os parâmetros offset e length |
Exemplos
Example#1 Exemplo da substr_count()
<?php
$text = 'This is a test';
echo strlen($text); // 14
echo substr_count($text, 'is'); // 2
// the string is reduced to 's is a test', so it prints 1
echo substr_count($text, 'is', 3);
// the text is reduced to 's i', so it prints 0
echo substr_count($text, 'is', 3, 3);
// generates a warning because 5+10 > 14
echo substr_count($text, 'is', 5, 10);
// prints only 1, because it doesn't count overlapped subtrings
$text2 = 'gcdgcdgcd';
echo substr_count($text2, 'gcdgcd');
?>