-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText.php
More file actions
106 lines (98 loc) · 2.54 KB
/
Text.php
File metadata and controls
106 lines (98 loc) · 2.54 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* This file is part of the Zimbra API in PHP library.
*
* © Nguyen Van Nguyen <nguyennv1981@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zimbra\Common;
/**
* Text class
*
* @package Zimbra
* @category Common
* @author Nguyen Van Nguyen - nguyennv1981@gmail.com
* @copyright Copyright © 2013 by Nguyen Van Nguyen.
*/
class Text
{
/**
* Returns true if the $haystack string begins with $needle, false otherwise.
*
* @param string $haystack.
* @param string $needle.
* @return bool
*/
public static function startsWith($haystack, $needle)
{
return $needle === '' || strpos($haystack, $needle) === 0;
}
/**
* Returns true if the $haystack string ends with $needle, false otherwise.
*
* @param string $haystack.
* @param string $needle.
* @return bool
*/
public function endsWith($haystack, $needle)
{
return $needle === '' || substr($haystack, -strlen($needle)) === $needle;
}
/**
* Check the string is rgb.
*
* @param string $tag The rgb string.
* @return bool
*/
public static function isRgb($rgb)
{
return (bool) preg_match('/^#([a-f0-9]{3}){1,2}$/iD', $rgb);
}
/**
* Check the tag is valid.
*
* @param string $tag The tag name.
* @return bool
*/
public static function isValidTagName($tag)
{
$pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i';
return preg_match($pattern, $tag, $matches) and $matches[0] == $tag;
}
/**
* Extract header string to array.
*
* @param string $headerString Header string.
* @return array
*/
public static function extractHeaders($headerString = '')
{
$parts = explode("\r\n", $headerString);
$headers = [];
foreach ($parts as $part)
{
$pos = strpos($part, ':');
if($pos)
{
$name = trim(substr($part, 0, $pos));
$value = trim(substr($part, ($pos + 1)));
$headers[$name] = $value;
}
}
return $headers;
}
/**
* Convert bool value to string.
*
* @param string $tag The tag name.
* @return string
*/
public static function boolToString($value)
{
$value = $value === true ? 'true' : $value;
$value = $value === false ? 'false' : $value;
return $value;
}
}