Monday, 31 January 2011

PHP: String StartsWith / EndsWith

In case you need the PHP equivalent to the .NET String.StartsWith and String.EndsWith, here they are:


public static function StringStartsWith($string, $search) {
 $ret = false;
 if (strlen($string) >= strlen($search)) {
  $ret = (substr($string, 0, strlen($search)) == $search);
 }
 return $ret;
}

public static function StringEndsWith($string, $search) {
 $string = strrev($string);
 $search = strrev($search);
 return Utils::StringStartsWith($string,$search);
}

Hope it helps :)

No comments:

Post a Comment