-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.php
125 lines (118 loc) · 3.09 KB
/
String.php
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
<?php
/**
* PHP String Helper
*
* Copyright (c) 2012 Brett O'Donnell <brett@mrphp.com.au>
* Source Code: https://github.com/cornernote/php-string
* Home Page: http://mrphp.com.au/blog/php-string-helper-class
* License: GPLv3
*/
class String
{
/**
* @param $contents
* @param $start
* @param $end
* @param bool $removeStart
* @return string
*/
public function getBetweenString($contents, $start, $end, $removeStart = false)
{
$startPos = strpos($contents, $start);
$endPos = strpos($contents, $end, $startPos);
if ($removeStart) {
$startPos += strlen($start);
}
$len = $endPos - $startPos;
$subString = substr($contents, $startPos, $len);
return $subString;
}
/**
* @static
* @param $fileName
* @return mixed
*/
public static function getValidFileName($fileName)
{
$fileName = preg_replace('/[^0-9a-z ]+/i', '', $fileName);
return $fileName;
}
/**
* @static
* @param $string
* @return mixed|string
*/
public static function slug($string)
{
$string = strtolower($string);
$string = preg_replace('/[^0-9a-z ]/', '', $string);
while (strpos($string, ' ') !== false) $string = str_replace(' ', ' ', $string);
$string = str_replace(' ', '-', $string);
return $string;
}
/**
* @param $haystack
* @param $needle
* @return bool
*/
public static function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
/**
* @param $haystack
* @param $needle
* @return bool
*/
public static function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
/**
* @param $string
* @return mixed
*/
public static function unserialize($string)
{
if (self::isSerialized($string)) {
return unserialize($string);
}
return $string;
}
/**
* @param $data
* @return bool
* @ref - http://stackoverflow.com/questions/1369936/check-to-see-if-a-string-is-serialized
*/
public static function isSerialized($data)
{
// if it isn't a string, it isn't serialized
if (!is_string($data))
return false;
$data = trim($data);
if ('N;' == $data)
return true;
if (!preg_match('/^([adObis]):/', $data, $badions))
return false;
switch ($badions[1]) {
case 'a' :
case 'O' :
case 's' :
if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data))
return true;
break;
case 'b' :
case 'i' :
case 'd' :
if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data))
return true;
break;
}
return false;
}
}