Skip to content

Instantly share code, notes, and snippets.

@umidjons
Created April 8, 2014 05:40
Show Gist options
  • Save umidjons/10094793 to your computer and use it in GitHub Desktop.
Save umidjons/10094793 to your computer and use it in GitHub Desktop.
PHP: startsWith & endsWith functions

startsWith & endsWith functions

<?php
function startsWith($haystack, $needle)
{
    return strncmp($haystack, $needle, strlen($needle)) === 0;
}

function endsWith($haystack, $needle)
{
    return $needle === '' || substr_compare($haystack, $needle, -strlen($needle)) === 0;
}

Link to discussion on StackOverflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment