Starts and Ends With

/**
 * Returns `true` if the string (haystack) starts with the search-string (needle).
 * See: https://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php
 */
function starts_with($haystack, $needle){
    return substr($haystack, 0, strlen($needle)) === $needle;
}

// Tests.
$string = 'Test test test this is a test.';

echo (starts_with($string, '27') ? 1 : 0); // 0
echo (starts_with($string, 'Rest') ? 1 : 0); // 0
echo (starts_with($string, 'Test') ? 1 : 0); // 1
echo (starts_with($string, 'test test test') ? 1 : 0); // 0
echo (starts_with($string, 'Test test test') ? 1 : 0); // 1
echo (starts_with($string, 'Test test test this is a test.') ? 1 : 0); // 1

// Lowercase both input vars to check with case-insensitivity.
echo (starts_with(strtolower($string), strtolower('Test')) ? 1 : 0); // 1
/**
 * Returns `true` if the string (haystack) ends with the search-string (needle).
 * See: https://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php
 */
function ends_with($haystack, $needle){
    $length = strlen($needle);
    if ($length == 0) {
        return true;
    }
    return (substr($haystack, -$length) === $needle);
}
/**
 * Return characters from the beginning or end of a string.
 */
$string = 'testers';

// Get the first character of a string.
$newString = substr( $string, 1 ); // returns "t"

// Get the first three characters of a string.
$newString = substr( $string, 3 ); // returns "tes"

// Get the last character of a string.
$newString = substr( $string, -1 ); // returns "s"

// Get the last three characters of a string.
$newString = substr( $string, -3 ); // returns "ers"
/**
 * Remove characters from the beginning or end of a string.
 */
$string = 'testers';

// Remove the first character from a string.
$newString = substr($string, 0, 1); // returns "esters"

// Remove the first three characters from a string.
$newString = substr($string, 0, -3); // returns "ters"

// Remove the last character from a string.
$newString = substr($string, 0, -1); // returns "tester"

// Remove the last three characters from a string.
$newString = substr($string, 0, -3); // returns "test"
/**
 * Returns all characters from the string after
 * the last occurrence of the substring.
 */
function get_after_last_occurance( $string, $substring ){
    return array_pop(explode( $substring, $string));
}