<?php

class StringHelper {

    public static function endsWith($haystack, $needle, $case = true)
    {
        if ($case) {
            return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
        }
        return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
    }
}


function startsWith($haystack, $needle)
{
	return !strncmp($haystack, $needle, strlen($needle));
}

function endsWith($haystack, $needle)
{
	$length = strlen($needle);
	if ($length == 0) {
		return true;
	}

	return (substr($haystack, -$length) === $needle);
}

function permute($word)
{
    if (!$word) {
        return array($word);
    }
    $maxLength = mb_strlen($word, 'UTF-8');

    $permutations = array();
    foreach (permute(uf8Substr($word, 1, $maxLength)) as $permutation) {
        $lower = mb_strtolower(uf8Substr($word), 'UTF-8');
        $permutations[] = $lower . $permutation;

        $upper = mb_strtoupper(uf8Substr($word), 'UTF-8');
        if ($upper !== $lower) {
            $permutations[] = $upper . $permutation;
        }
    }
    return $permutations;
}

function uf8Substr($word, $start = 0, $length = 1)
{
    return mb_substr($word, $start, $length, 'UTF-8');
}
