Skip to content

Instantly share code, notes, and snippets.

@xexu
Last active August 29, 2015 13:58
Show Gist options
  • Select an option

  • Save xexu/9972135 to your computer and use it in GitHub Desktop.

Select an option

Save xexu/9972135 to your computer and use it in GitHub Desktop.

Revisions

  1. xexu renamed this gist Apr 4, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. xexu created this gist Apr 4, 2014.
    18 changes: 18 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    //http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions/834355#834355
    //Note: Be careful with empty strings, you might want a different behaviour

    function startsWith($haystack, $needle)
    {
    $length = strlen($needle);
    return (substr($haystack, 0, $length) === $needle);
    }

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

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