When you came to this page, you may have been looking for something a little simpler: A function that can check if a small string exists within a larger string starting at a particular index. Using substr_compare() for this can leave your code messy, because you need to check that your string is long enough (to avoid the warning), manually specify the length of the short string, and like many of the string functions, perform an integer comparison to answer a true/false question.
I put together a simple function to return true if $str exists within $mainStr. If $loc is specified, the $str must begin at that index. If not, the entire $mainStr will be searched.
<?php
function contains_substr($mainStr, $str, $loc = false) {
if ($loc === false) return (strpos($mainStr, $str) !== false);
if (strlen($mainStr) < strlen($str)) return false;
if (($loc + strlen($str)) > strlen($mainStr)) return false;
return (strcmp(substr($mainStr, $loc, strlen($str)), $str) == 0);
}
?>