1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* Truncates a $string to the specified $length.
|
5
|
* If the supplied string is equal to or shorter than the $legth the original is returend.
|
6
|
* if an $appendix is defined the resulting string will have the specified $length including the $appendix.
|
7
|
*
|
8
|
* @param String $string the string to truncate
|
9
|
* @param Number $length the maximun length
|
10
|
* @param String $appendix an optional appendix.
|
11
|
*
|
12
|
* @return the string
|
13
|
*/
|
14
|
function str_trunk(&$string, $length, $appendix=''){
|
15
|
if(strlen($string) >= $length ){
|
16
|
return substr($string, 0, $length - strlen($appendix)).$appendix;
|
17
|
} else {
|
18
|
return $string;
|
19
|
}
|
20
|
}
|
21
|
|
22
|
/**
|
23
|
* @param string $str
|
24
|
* @param string $sub
|
25
|
* @return boolean
|
26
|
*/
|
27
|
function str_beginsWith( $str, $sub ) {
|
28
|
return ( substr( $str, 0, strlen( $sub ) ) === $sub );
|
29
|
}
|
30
|
|
31
|
/**
|
32
|
*
|
33
|
* @param string $str
|
34
|
* @param string $sub
|
35
|
* @return boolean
|
36
|
*/
|
37
|
function str_endsWith( $str, $sub ) {
|
38
|
return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* Replaces all occurences in $array of the key defined in $replace_map with the according values in the
|
43
|
* $replace_map.
|
44
|
*
|
45
|
* @param $array
|
46
|
* @param $replace_map
|
47
|
* @return unknown_type
|
48
|
*/
|
49
|
function array_replace_key($array, $replace_map){
|
50
|
foreach($replace_map as $key=>$newkey){
|
51
|
if(isset($array[$key])){
|
52
|
$array[$newkey] = $array[$key];
|
53
|
unset($array[$key]);
|
54
|
}
|
55
|
}
|
56
|
return $array;
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* Replaces all occurrences of space characters with
|
61
|
* an underscore and transforms the given
|
62
|
* string to lowercase.
|
63
|
*
|
64
|
* @param String $string
|
65
|
* @return the transformed string
|
66
|
*/
|
67
|
function generalizeString($string){
|
68
|
return str_replace(' ', '_', strtolower($string));
|
69
|
}
|