How to check how many times something occurs in a string in PHP

前端 未结 5 1027
眼角桃花
眼角桃花 2020-12-19 18:56

I want to check how many instances of the letter a appears in a certain string.

What is the function for this? I need it to return an integer value.

5条回答
  •  一整个雨季
    2020-12-19 19:38

    substr_count is probably the most straightforward. you can also do this

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

提交回复
热议问题