Ubiquity  2.0.3
php rapid development framework
UString.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils\base;
4 
10 class UString {
11 
12  public static function startswith($hay, $needle) {
13  return \substr($hay, 0, strlen($needle)) === $needle;
14  }
15 
16  public static function endswith($hay, $needle) {
17  return \substr($hay, -strlen($needle)) === $needle;
18  }
19 
20  public static function getBooleanStr($value) {
21  $ret="false";
22  if ($value)
23  $ret="true";
24  return $ret;
25  }
26 
27  public static function isNull($s) {
28  return (!isset($s) || NULL === $s || "" === $s);
29  }
30 
31  public static function isNotNull($s) {
32  return (isset($s) && NULL !== $s && "" !== $s);
33  }
34 
35  public static function isBooleanTrue($s) {
36  return filter_var($s, FILTER_VALIDATE_BOOLEAN)===true;
37  }
38 
39  public static function isBooleanFalse($s) {
40  return $s === false || $s === "false" || $s === 0 || $s === "0";
41  }
42 
43  public static function isBoolean($value) {
44  return \is_bool($value);
45  }
46 
47  public static function isBooleanStr($value) {
48  return \is_bool($value) || $value===0 || $value===1 || $value==="on";
49  }
50 
59  public static function pluralize($count, $zero, $one,$other) {
60  $result="";
61  if($count===0){
62  $result=$zero;
63  }elseif($count===1){
64  $result=$one;
65  }else{
66  $result=$other;
67  }
68  return \str_replace('{count}', $count, $result);
69  }
70 
71  public static function firstReplace($haystack, $needle, $replace) {
72  $newstring=$haystack;
73  $pos=strpos($haystack, $needle);
74  if ($pos !== false) {
75  $newstring=\substr_replace($haystack, $replace, $pos, strlen($needle));
76  }
77  return $newstring;
78  }
79 
80  public static function replaceFirstOccurrence($pattern, $replacement, $subject){
81  $pattern = '/'.preg_quote($pattern, '/').'/';
82  return preg_replace($pattern, $replacement, $subject, 1);
83  }
84 
85  public static function replaceArray($haystack, $needle, $replaceArray) {
86  $result=$haystack;
87  foreach ( $replaceArray as $replace ) {
88  $result=self::firstReplace($result, $needle, $replace);
89  }
90  return $result;
91  }
92 
93  public static function doubleBackSlashes($value){
94  if(is_string($value))
95  return str_replace("\\", "\\\\", $value);
96  return $value;
97  }
98 
99  public static function cleanAttribute($attr, $replacement="_") {
100  $result=preg_replace('/[^a-zA-Z0-9\-]/s', $replacement, $attr);
101  return \str_replace($replacement . $replacement, $replacement, $result);
102  }
103 }
104 
static firstReplace($haystack, $needle, $replace)
Definition: UString.php:71
static doubleBackSlashes($value)
Definition: UString.php:93
static pluralize($count, $zero, $one, $other)
Pluralize an expression.
Definition: UString.php:59
$replace
Definition: traits.php:14
String utilities.
Definition: UString.php:10
static cleanAttribute($attr, $replacement="_")
Definition: UString.php:99
static getBooleanStr($value)
Definition: UString.php:20
static replaceArray($haystack, $needle, $replaceArray)
Definition: UString.php:85
static replaceFirstOccurrence($pattern, $replacement, $subject)
Definition: UString.php:80
static endswith($hay, $needle)
Definition: UString.php:16
static isBooleanStr($value)
Definition: UString.php:47
static isBoolean($value)
Definition: UString.php:43
static startswith($hay, $needle)
Definition: UString.php:12