function startsWith($haystack, $needle) { // search backwards starting from haystack length characters from the end return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;}function endsWith($haystack, $needle) { // search forward starting from end minus needle length characters return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);}
startsWith("abcdef", "ab") -> truestartsWith("abcdef", "cd") -> falsestartsWith("abcdef", "ef") -> falsestartsWith("abcdef", "") -> truestartsWith("", "abcdef") -> falseendsWith("abcdef", "ab") -> falseendsWith("abcdef", "cd") -> falseendsWith("abcdef", "ef") -> trueendsWith("abcdef", "") -> trueendsWith("", "abcdef") -> false
Source page: