Şey PHP bir dizesinde kaç kere meydana geldiğine nasıl kontrol

5 Cevap php

Ben Mektubun a, belirli bir dize görünür kaç örnekleri kontrol etmek istiyorum.

Bunun için fonksiyonu nedir? Ben bir tamsayı değeri döndürmek gerekir.

5 Cevap

Sen işlevini kullanabilirsiniz: substr_count

Örnek:

$str = "I love stackoverflow";
echo substr_count($str,'o'); // prints 3

I substr_count hile yapabilir herhalde ;-)


For example, this portion of code :

$str = 'abcdazerty';
echo substr_count($str, 'a');

Size aşağıdaki çıktıyı almak istiyorum:

2


And, quoting :

int substr_count  (  string $haystack  ,  
    string $needle  [,  int $offset = 0  
    [,  int $length  ]] )

substr_count() returns the number of times the needle substring occurs in the haystack string. Please note that needle is case sensitive.

Eğer gereken herhangi bir fonksiyonu bulmak için PHP string fonksiyonları kılavuzuna başvurabilirsiniz, http://php.net/strings

substr_count muhtemelen en basit. Ayrıca bu yapabilirsiniz

$str = "I love stackovaerflowa";
print count(explode("a",$str))-1;

substr_count($haystack, $needle);