This repository was archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStrUtil.php
123 lines (115 loc) · 3.41 KB
/
StrUtil.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
<?php
/* THIS FILE:
* a) BELONGS TO THE 'PHP-UTIL' LIBRARY:
* https://github.com/thiagodp/php-util
*
* b) IS DISTRIBUTED UNDER THE CREATIVE COMMONS LICENCE (CC BY 3.0):
* http://creativecommons.org/licenses/by/3.0/
*
* USE IT AT YOUR OWN RISK!
*/
/**
* Useful string functions.
*
* @author Thiago Delgado Pinto
* @version 1.0
*/
class StrUtil {
/**
* Transform a snake_case string into a camelCase string.
*
* @param string $snake the string to convert
* @return string
*/
static function snakeToCamel( $snake ) {
$pieces = explode( '_', $snake );
$count = count( $pieces );
if ( 1 == $count ) {
return $snake;
}
$str = $pieces[ 0 ];
for ( $i = 1; $i < $count; ++$i ) {
$p = $pieces[ $i ];
$str .= mb_strtoupper( $p[ 0 ] ) . mb_substr( $p, 1 );
}
return $str;
}
/**
* Transform a camelCase string into a snake_case string.
*
* @param string $camel the string to convert
* @return string
*/
static function camelToSnake( $camel ) {
return mb_strtolower( preg_replace( '/([A-Z])/u', "_$1", $camel ) );
}
/**
* Transform a text to uppercase.
*
* @deprecated Use {@link mb_strtoupper} instead.
*
* @param text the text to transform.
* @param encoding the character encoding to use. default is {@link mb_internal_encoding()}.
* @return the text in uppercase.
*/
static function toUpper( $text, $encoding = null ) {
$enc = is_string( $encoding ) ? $encoding : mb_internal_encoding();
return mb_strtoupper( $text, $enc );
}
/**
* Transform a text to lowercase.
*
* @deprecated Use {@link mb_strtolower} instead.
*
* @param text the text to transform.
* @param encoding the character encoding to use. default is {@link mb_internal_encoding()}.
* @return the text in lowercase.
*/
static function toLower( $text, $encoding = null ) {
$enc = is_string( $encoding ) ? $encoding : mb_internal_encoding();
return mb_strtolower( $text, $enc );
}
/**
* Transform all the letters to lowercase and just the first letter of each word to uppercase.
*
* This method can be replaced by {@code mb_convert_case( 'your text here', MB_CASE_TITLE )} when
* the {@code exceptions} parameter is not given.
*
* How to use it:
* #( 'john von neumann', array( ' von ' ) ) ==> 'John von Neumann'
* #( 'JOHN VON NEUMANN', array( ' von ' ) ) ==> 'John von Neumann'
* #( 'maria da silva e castro', array( ' da ', ' e ' ) ) ==> 'Maria da Silva e Castro'
*
* @see {@link commonNameExceptions}.
*
* @param text text to transform.
* @param exceptions array of words to ignore exceptions.
* @param encoding the character encoding to use. default is {@link mb_internal_encoding()}.
* @return the transformed text.
*/
static function upperCaseFirst(
$text,
array $exceptions = array(),
$encoding = null
) {
$enc = is_string( $encoding ) ? $encoding : mb_internal_encoding();
$newText = mb_convert_case( $text, MB_CASE_TITLE, $enc );
if ( count( $exceptions ) < 1 ) {
return $newText;
}
foreach ( $exceptions as $e ) {
$newText = str_replace( mb_convert_case( $e, MB_CASE_TITLE, $enc ),
mb_convert_case( $e, MB_CASE_LOWER, $enc ), $newText );
}
return $newText;
}
/** Common name separators to be used as exceptions with {@link upperCaseFirst}. */
static function commonNameSeparators() {
return array(
' de ', ' da ', ' di ', ' del ', ' della ',
' e ', ' i ', ' y ',
' van ', ' von '
);
}
}
?>