forked from phpgearbox/string
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartEndWith.php
More file actions
79 lines (71 loc) · 2.87 KB
/
Copy pathStartEndWith.php
File metadata and controls
79 lines (71 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php namespace Gears\String\Methods;
////////////////////////////////////////////////////////////////////////////////
// __________ __ ________ __________
// \______ \ |__ ______ / _____/ ____ _____ ______\______ \ _______ ___
// | ___/ | \\____ \/ \ ____/ __ \\__ \\_ __ \ | _// _ \ \/ /
// | | | Y \ |_> > \_\ \ ___/ / __ \| | \/ | ( <_> > <
// |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \
// \/|__| \/ \/ \/ \/ \/
// -----------------------------------------------------------------------------
// Designed and Developed by Brad Jones <brad @="bjc.id.au" />
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
use voku\helper\UTF8;
trait StartEndWith
{
/**
* Returns true if the string begins with $substring, false otherwise.
*
* By default, the comparison is case-sensitive,
* but can be made insensitive by setting $caseSensitive
* to false.
*
* @param string $substring The substring to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
*
* @return bool Whether or not $str starts with $substring
*/
public function startsWith($substring, $caseSensitive = true)
{
$startOfStr = UTF8::substr
(
$this->scalarString,
0,
UTF8::strlen($substring, $this->encoding),
$this->encoding
);
if (!$caseSensitive)
{
$substring = UTF8::strtolower($substring, $this->encoding);
$startOfStr = UTF8::strtolower($startOfStr, $this->encoding);
}
return (string)$substring === $startOfStr;
}
/**
* Returns true if the string ends with $substring, false otherwise. By
* default, the comparison is case-sensitive, but can be made insensitive
* by setting $caseSensitive to false.
*
* @param string $substring The substring to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
*
* @return bool Whether or not $str ends with $substring
*/
public function endsWith($substring, $caseSensitive = true)
{
$substringLength = UTF8::strlen($substring, $this->encoding);
$endOfStr = UTF8::substr
(
$this->scalarString,
$this->getLength() - $substringLength,
$substringLength,
$this->encoding
);
if (!$caseSensitive)
{
$substring = UTF8::strtolower($substring, $this->encoding);
$endOfStr = UTF8::strtolower($endOfStr, $this->encoding);
}
return (string)$substring === $endOfStr;
}
}