forked from surebert/surebert-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrings.php
More file actions
153 lines (132 loc) · 3.69 KB
/
Strings.php
File metadata and controls
153 lines (132 loc) · 3.69 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Used to parse strings
*
* @author Paul Visco
* @version 1.02 09/17/2007
* @package sb_Strings
*/
class sb_Strings{
/**
* Strips punctuation from a string
*
* @author Paul Visco
* @param string $str The string to strip punctuation from. Allows space, underscore, numbers and letters both lowercase and capital
* @return string
* @version 2.1
*
*/
public static function strip_punct($str){
return preg_replace("~[^ \w]~", "", $str);
}
/**
* Converts underscore separated word strings into camel style e.g. to_camel becomes toCamel
*
* @author Paul Visco
* @param string $str
* @return string
*/
public static function to_camel($str){
return str_replace(' ', '', ucwords(preg_replace('/[^A-Z^a-z^0-9]+/', ' ', $str)));
}
/**
* Cleans up file names and removes extraneous spaces, symbols, etc
*
* @author Paul Visco
* @param string $str
* @return string
*/
public static function clean_file_name($str){
preg_match('~\.\w{1,4}$~', $str, $ext);
$str = preg_replace('~\.\w{1,4}$~', '', $str);
return str_replace(Array(' ', '.', '-'), "_", $str).$ext[0];
}
/**
* Escapes html tags to make them safe to print to screen
*
* @author Paul Visco
* @param string $str
* @return string
*/
public static function html_escape_tags($str){
return preg_replace("/&(#[0-9]+|[a-z]+);/i", "&$1;", htmlspecialchars($str));
}
/**
* Removes microsoft characters and replace them with their ascii counterparts
*
* @author Paul Visco
* @param string $str
* @return string
*/
public static function strip_microsoft_chars($str){
$chars=array(
chr(133) => '...', //dot dot dot elipsis
chr(145) => "'", //curly left single quotes
chr(146) => "'", //curly right single quotes
chr(147) => '"', //curly left double quotes
chr(148) => '"', //curly right double quotes
chr(149) => '*', //bullet char
chr(150) => '-', //en dash
chr(151) => '-', //em dash,
//mac word roman charset
chr(165) => '*', //bullet char
chr(201) => '...', //elipsis
chr(208) => '-', //en-dash
chr(209) => '-', //em-dash
chr(210) => '"', //curly left double quotes
chr(211) => '"', //curly right double quotes
chr(212) => '\'', //curly left single quote
chr(213) => '\'', //curly right single quotes
);
return strtr($str, $chars);
}
/**
* replaces unicode characters with ascii in unicode encoded urls e.g.from ajax calls
*
* @author Paul Visco
* @param string $str
* @return string
*/
public static function unicode_urldecode($str){
preg_match_all('/%u([[:alnum:]]{4})/', $str, $matches);
foreach ($matches[1] as $uniord){
$str = str_replace('%u'.$uniord, '&#x' . $uniord . ';', $str);
}
return urldecode($str);
}
/**
* Add an s to a work if there is only one of them
*
* @author Paul Visco
* @param integer $quanitity
* @param string $noun
* @return string
*/
public static function pluralize($quanitity, $noun){
$quanitity = intval($quanitity);
return ($quanitity ===1) ? $noun : $noun.'s';
}
/**
* Truncates a string to a certain length and right padswith ...
*
* @param string $str
* @param integer $maxlength The maximum length, keep in mind that the ... counts as three chars
* @return string
*/
public static function truncate($str, $maxlength=20){
if(strlen($str) > $maxlength){
$str = substr($str, 0, $maxlength-3).'...';
}
return $str;
}
/**
* Creates a string that is safe to display as HTML by stripping all tags, using html entities and removing slashes
*
* @param string $str
* @return string
*/
public static function safe($str){
return htmlentities(strip_tags(stripslashes(($str))));
}
}
?>