substr_count

  • Counting the occurrences of a substring within a string of characters id done in Ruby using a combination of String#scan to scan for the word and Array#length to count the occurrences.

    PHP

    print substr_count("the cat in the hat", "the");
    // => 2

    Ruby

    puts "the cat in the hat".scan("the").length
    # => 2

    see also

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.