/
String.class.php
136 lines (100 loc) · 2.74 KB
/
String.class.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
126
127
128
129
130
131
132
<?php
/*
** Fonctions qui gère les chaines de caractères.
**
** @package Ninaca
** @author Nivl <nivl@free.fr>
** @started 08/29/2009, 04:07 PM
** @last Nivl <nivl@free.fr> 03/28/2010, 03:46 PM
** @link http://nivl.free.fr
** @copyright Copyright (C) 2009 Laplanche Melvin
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class String
{
static public function clean($val)
{
return trim($val, " \t\n\r\0\x7f..\xff\x0..\x1f");
}
static public function length($str)
{
return mb_strlen($str);
}
static public function lcFirst($str)
{
if ( !empty($str) )
$str[0] = mb_strtolower($str[0]);
return $str;
}
static public function lower($str)
{
return mb_strtolower($str);
}
static public function isLower($str)
{
return $str == mb_strtoupper($str);
}
static public function ucFirst($str)
{
if ( !empty($str) )
$str[0] = mb_strtoupper($str[0]);
return $str;
}
static public function upper($str)
{
return mb_strtoupper($str);
}
static public function isUpper($str)
{
return $str === mb_strtoupper($str);
}
static public function wrap80($string, $nl = '')
{
$strings = explode(PHP_EOL, $string);
foreach ( $strings as $key => $str )
{
$str = wordwrap($str, 80, PHP_EOL);
$strings[$key] = str_replace(PHP_EOL, PHP_EOL.$nl, $str);
}
return implode(PHP_EOL, $strings);
}
static function countCharOccurrence($char, $string)
{
if ( Misc::isEmpty($char) || Misc::isEmpty($string) )
return false;
$len = self::lenght($string);
$occ = 0;
for ( $i = 0; $i<$len; ++$i)
{
if ( $string[$i] == $char )
++$occ;
}
return $occ;
}
static function substr($string, $start, $length = null)
{
if ( $length === null )
$length = String::length($string) - $start;
return mb_substr($string, $start, $length);
}
static function strstr($haystack, $needle, $part = false)
{
return mb_strstr($haystack, $needle, $part);
}
static function strpos($haystack, $needle, $offset = 0)
{
return mb_strpos($haystack, $needle, $offset);
}
}