28. Find the Index of the First Occurrence in a String

class Solution {

    /**
     * @param String $haystack
     * @param String $needle
     * @return Integer
     */
    function strStr($haystack, $needle) {
        $matches = [];
        $result = preg_match("/$needle/", $haystack, $matches, PREG_OFFSET_CAPTURE);
        return $result
            ? $matches[0][1] 
            : -1;
    }
}

Last updated