strpos

  • To find the position of a substring within a string of characters, we can use Ruby’s String#index method.

    PHP

    print strpos("hello world", "wo");
    // => 6

    Ruby

    p "hello world".index("wo")
    # => 6

    The strpos function is often used to simply check for the presence of a substring within a string. This is usage is accomplished in Ruby by using the String#include? method.

    PHP

    $result = (strpos("hello world", "wo") !== false);
    var_export($result);
    // => true

    Ruby

    puts "hello world".include?("wo")
    # => true

    see also

strlen strrev

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.