Modified version of the original posted function. Use this one:
<?php
if (!function_exists('substr_compare')) {
function substr_compare($main_str, $str, $offset, $length = NULL, $case_insensitivity = false) {
$offset = (int) $offset;
// Throw a warning because the offset is invalid
if ($offset >= strlen($main_str)) {
trigger_error('The start position cannot exceed initial string length.', E_USER_WARNING);
return false;
}
// We are comparing the first n-characters of each string, so let's use the PHP function to do it
if ($offset == 0 && is_int($length) && $case_insensitivity === true) {
return strncasecmp($main_str, $str, $length);
}
// Get the substring that we are comparing
if (is_int($length)) {
$main_substr = substr($main_str, $offset, $length);
$str_substr = substr($str, 0, $length);
} else {
$main_substr = substr($main_str, $offset);
$str_substr = $str;
}
// Return a case-insensitive comparison of the two strings
if ($case_insensitivity === true) {
return strcasecmp($main_substr, $str_substr);
}
// Return a case-sensitive comparison of the two strings
return strcmp($main_substr, $str_substr);
}
}
?>