1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* FUNÇÕES DE TRATAMENTO DE STRINGS |
5
|
|
|
* Funções que tratam uma string e a retorna modificada começam com nome: str[...] |
6
|
|
|
* Funções que convertem uma string para outro formato começam com nome: strTo[...] |
7
|
|
|
* Funções de conversão deve ter o nome no formato: [...]To[...] |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
setlocale(LC_ALL, 'pt_BR.UTF8'); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Corta um texto, sem cortar a última palavra. |
14
|
|
|
* @param string $string [string a ser cortada] |
15
|
|
|
* @param int $length [tamanho da string cortada] |
16
|
|
|
* @param bool $rep [define se corta antes ou depois do tamanho maximo] |
17
|
|
|
* @return string $string [string resumida ] |
18
|
|
|
*/ |
19
|
|
|
function strTruncate($string, $length, $rep = false) { |
20
|
|
|
if (strlen($string) <= $length) { |
21
|
|
|
return $string; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if ($rep == true) { |
|
|
|
|
25
|
|
|
$oc = strrpos(substr($string, 0, $length), ' '); |
26
|
|
|
} |
27
|
|
|
if ($rep == false) { |
|
|
|
|
28
|
|
|
$oc = strpos(substr($string, $length), ' ') + $length; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$string = substr($string, 0, $oc) . '...'; |
|
|
|
|
32
|
|
|
return $string; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Limpa a string de caracteres inválidos |
37
|
|
|
* @param string $string |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
function strClear($string) { |
41
|
|
|
return trim(strip_tags(str_replace('"', '"', $string))); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Criptografa a string utilizando MD5 + SALT |
46
|
|
|
* @param string $string [string a ser criptografada] |
47
|
|
|
* @return string $stringEncript [string criptografada] |
48
|
|
|
*/ |
49
|
|
|
function strEncrypt($string, $salt = APP_ID) { |
|
|
|
|
50
|
|
|
return md5($salt . $string); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retorna total de caracteres da string UTF-8 |
55
|
|
|
* @param string |
56
|
|
|
* @return int |
57
|
|
|
*/ |
58
|
|
|
function strLength($string) { |
59
|
|
|
return mb_strlen($string); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Retorna a string Maiúscula UTF-8 |
64
|
|
|
* @param string $string |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
function strUpper($string) { |
68
|
|
|
return mb_strtoupper($string); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Retorna a string Minúscula UTF-8 |
73
|
|
|
* @param string $string |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
function strLower($string) { |
77
|
|
|
return mb_strtolower($string); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Retorna a string somente com a primeira letra maiúscula UTF-8 |
82
|
|
|
* @param string $string |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
function strFirst($string) { |
86
|
|
|
return mb_strtoupper(mb_substr($string, 0, 1)) . mb_strtolower(mb_substr($string, 1)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Limpa a string, retirando espaços e tags html |
91
|
|
|
* @param string $string |
92
|
|
|
* @return string [retorna string limpa] |
93
|
|
|
*/ |
94
|
|
|
function strStrip($string) { |
95
|
|
|
return trim(strip_tags($string)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Formata o número com zeros à esquerda |
100
|
|
|
* @param int $int [numero a ser formatado] |
101
|
|
|
* @param int $length [quantidade de caracteres que o numero deverá ter] |
102
|
|
|
* @return string [número formatado. Ex: 01, 02 , 001, 003] |
103
|
|
|
*/ |
104
|
|
|
function strLengthFormat($int = 0, $length = 2) { |
105
|
|
|
return str_pad($int, $length, "0", STR_PAD_LEFT); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Converte uma string, nome ou frase em URL válida |
110
|
|
|
* @param string $string [Ex: Minha Notícia da 'Página 2000 especial' ] |
111
|
|
|
* @return string $url [Ex: minha-noticia-da-pagina-2000-especial ] |
112
|
|
|
*/ |
113
|
|
|
function strToURL($string) { |
114
|
|
|
$url = iconv('UTF-8', 'ASCII//TRANSLIT', $string); |
115
|
|
|
$url = preg_replace("/[^a-zA-Z0-9\/_| -]/", '', $url); |
116
|
|
|
$url = strtolower(trim($url, '-')); |
117
|
|
|
$url = preg_replace("/[\/_| -]+/", '-', $url); |
118
|
|
|
return $url; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Converte String para Float |
123
|
|
|
* @param string $string |
124
|
|
|
* @return float |
125
|
|
|
*/ |
126
|
|
|
function strToFloat($string) { |
127
|
|
|
$f = str_replace(array('.', 'R$', '%'), array('', '', ''), $string); |
128
|
|
|
return (float) str_replace(',', '.', $f); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Converte float para Moeda (em Real R$) |
133
|
|
|
* @param float $float |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
function floatToMoney($float) { |
137
|
|
|
return number_format((float) $float, 2, ',', '.'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Converte boolean para String |
142
|
|
|
* @param boolean $boolean |
143
|
|
|
* @return string (Sim/Não) |
144
|
|
|
*/ |
145
|
|
|
function booleanToString($boolean) { |
146
|
|
|
return ($boolean) ? 'Sim' : 'Não'; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Retorna no formato de ID, ex: #00009 |
151
|
|
|
* @param int |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
function formatId($id = 0) { |
155
|
|
|
return '#' . strLengthFormat($id, 6); |
156
|
|
|
} |
157
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.