How to count how many times a substring occurs in a string in PHP?

How to count how many times a substring occurs in a string in PHP?

Asked on November 3, 2018 in PHP.
Add Comment


  • 2 Answer(s)

    Here the PHP substr_count() function is used.

    The PHP substr_count() function is used to find out how many times a given substring is appears or repeated inside a string.

    For example:

    <?php
    $quote = "Try not to become a man of success, but rather try to become a man of value.";
     
    echo substr_count($quote, 'man'); // Outputs: 2
    ?>
    
    Answered on November 3, 2018.
    Add Comment

    You can use the function : substr_count

    Example:

    $str = "I love stackoverflow";
    echo substr_count($str,'o'); // prints 3
    Answered on February 25, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.