-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.power
More file actions
74 lines (64 loc) · 1.83 KB
/
code.power
File metadata and controls
74 lines (64 loc) · 1.83 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
/**
* The field builder switch
*
* @since 3.0.9
*/
protected static $builder = false;
/**
* Making field names safe
*
* @input string The string you would like to make safe
* @input boolean The switch to return an ALL UPPER CASE string
* @input string The string to use in white space
*
* @returns string on success
*
* @since 3.0.9
*/
public static function safe($string, $allcap = false, $spacer = '_')
{
// get global value
if (self::$builder === false)
{
self::$builder = Helper::getParams()->get('field_name_builder', 1);
}
// use the new convention
if (2 == self::$builder)
{
// 0nly continue if we have a string
if (StringHelper::check($string))
{
// check that the first character is not a number
if (is_numeric(substr((string)$string, 0, 1)))
{
$string = StringHelper::numbers($string);
}
// remove all other strange characters
$string = trim((string) $string);
$string = preg_replace('/'.$spacer.'+/', ' ', $string);
$string = preg_replace('/\s+/', ' ', $string);
// Transliterate string
$string = StringHelper::transliterate($string);
// remove all and keep only characters and numbers
$string = preg_replace("/[^A-Za-z0-9 ]/", '', (string) $string);
// replace white space with underscore (SAFEST OPTION)
$string = preg_replace('/\s+/', (string) $spacer, $string);
// return all caps
if ($allcap)
{
return strtoupper($string);
}
// default is to return lower
return strtolower($string);
}
// not a string
return '';
}
// return all caps
if ($allcap)
{
return StringHelper::safe($string, 'U');
}
// use the default (original behavior/convention)
return StringHelper::safe($string);
}