1: <?php
2:
3: /*
4: * Copyright (c) 2016 Geraldo B. Landre
5: *
6: * See the file LICENSE for copying permission.
7: */
8:
9: namespace utils;
10:
11: /**
12: * Description of String
13: *
14: * @author geraldo
15: */
16: class String {
17: // startsWith and endsWith from
18: // http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php
19:
20: /**
21: * Search backwards starting from haystack length characters from the end
22: * @assert("abcdef", "cd") == false
23: * @assert("abcdef", "ef") == false
24: * @assert("abcdef", "") == true
25: * @assert("", "abcdef") == false
26: *
27: * @link http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php font.
28: * @param string $haystack
29: * @param string $needle
30: * @return bool
31: */
32: public static function startsWith($haystack, $needle) {
33: return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
34: }
35:
36: /**
37: * Search forward starting from end minus needle length characters
38: *
39: * @assert("abcdef", "ab") == false
40: * @assert("abcdef", "cd") == false
41: * @assert("abcdef", "ef") == true
42: * @assert("abcdef", "") == true
43: * @assert("", "abcdef") == false
44: *
45: * @link http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php font.
46: * @param string $haystack
47: * @param string $needle
48: * @return bool
49: */
50: public static function endsWith($haystack, $needle) {
51: return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
52: }
53:
54: }
55: