-
Notifications
You must be signed in to change notification settings - Fork 22
/
String.inc
128 lines (121 loc) · 3.3 KB
/
String.inc
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* @file
*
*/
/**
* Checks the given string to see if it could repersent a boolean value based on the specified matches.
*
* @param string $string
* The string to check.
* @param array $matches
* The matches to compare the string with, if none are provided 'true' and 'false' will be used.
* @param boolean $case_sensitive
* Do a case sensitive compare? Defaults to false.
*
* @return boolean
* TRUE if the string repersents a boolean value defined by matches, FALSE other wise.
*/
function is_boolean_string($string, array $matches = NULL, $case_sensitive = FALSE) {
$matches = isset($matches) ? $matches : array('true', 'false');
$cmp_func = ($case_sensitive) ? 'strcmp' : 'strcasecmp';
foreach ($matches as $match) {
if ($cmp_func($string, $match) === 0) {
return TRUE;
}
}
return FALSE;
}
/**
* Attempts to cast the given string to a boolean value based on matched values.
*
* @param string $string
* The string to cast to a bool.
* @param array $matches
* The matches to compare the string with, where the key is used to compare and the value is the boolean
* value it repersents. If none are provided 'true' and 'false' will be used.
* @param boolean $case_sensitive
* Do a case sensitive compare? Defaults to false.
*
* @return mixed
* If the conversion is possible a boolean value repersented by the string is returned.
* If not possible then the string is returned intact.
*/
function cast_string_to_boolean($string, array $matches = NULL, $case_sensitive = FALSE) {
$matches = isset($matches) ? $matches : array(
'true' => TRUE,
'false' => FALSE,
);
$cmp_func = ($case_sensitive) ? 'strcmp' : 'strcasecmp';
foreach ($matches as $match => $value) {
if ($cmp_func($string, $match) === 0) {
return $value;
}
}
return $string;
}
/**
* Attempts to cast the given string to a particular type value based on matched values.
*
* @param string $string
* The string to cast to a type.
*
* @return mixed
* If the string repersents a literal type than convert to that type, other wise return the given string intact.
*/
function cast_string_to_type($string) {
if (is_string($string)) {
$matches = array(
'true' => TRUE,
'false' => FALSE,
'null' => NULL,
);
foreach ($matches as $match => $value) {
if (strcasecmp($string, $match) === 0) {
return $value;
}
}
}
return $string;
}
/**
* Attempts to cast the given type to a string.
*
* @param string $string
* The string to cast to a type.
*
* @return mixed
* If the string repersents a literal type than convert to that type, other wise return the given string intact.
*/
function cast_type_to_string($type) {
if (is_bool($type)) {
$type = $type ? 'TRUE' : 'FALSE';
}
if (!isset($type)) {
$type = 'NULL';
}
return (string) $type;
}
/**
* Appends a value to each new line in the given string.
*
* @param string $string
* @param string $appendage
*
* @return string
*/
function append_to_new_line($string, $appendage) {
$output = '';
foreach (preg_split("/(\r?\n)/", $string) as $line) {
$output .= $line . $appendage;
}
return $output;
}
/**
*
* @param string $string
* @return boolean
*/
function is_non_empty_string($string) {
return is_string($string) && trim($string) != '';
}