Ubiquity  2.0.2
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 $s === true || $s === "true" || $s === 1 || $s === "1";
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 
55  public static function pluralize($count, $zero, $one,$other) {
56  $result="";
57  if($count===0){
58  $result=$zero;
59  }elseif($count===1){
60  $result=$one;
61  }else{
62  $result=$other;
63  }
64  return \str_replace('{count}', $count, $result);
65  }
66 
67  public static function firstReplace($haystack, $needle, $replace) {
68  $newstring=$haystack;
69  $pos=strpos($haystack, $needle);
70  if ($pos !== false) {
71  $newstring=\substr_replace($haystack, $replace, $pos, strlen($needle));
72  }
73  return $newstring;
74  }
75 
76  public static function replaceArray($haystack, $needle, $replaceArray) {
77  $result=$haystack;
78  foreach ( $replaceArray as $replace ) {
79  $result=self::firstReplace($result, $needle, $replace);
80  }
81  return $result;
82  }
83 
84  public static function cleanAttribute($attr, $replacement="_") {
85  $result=preg_replace('/[^a-zA-Z0-9\-]/s', $replacement, $attr);
86  return \str_replace($replacement . $replacement, $replacement, $result);
87  }
88 }
89 
static firstReplace($haystack, $needle, $replace)
Definition: UString.php:67
static pluralize($count, $zero, $one, $other)
Pluralize an expression.
Definition: UString.php:55
$replace
Definition: traits.php:14
String utilities.
Definition: UString.php:10
static cleanAttribute($attr, $replacement="_")
Definition: UString.php:84
static getBooleanStr($value)
Definition: UString.php:20
static replaceArray($haystack, $needle, $replaceArray)
Definition: UString.php:76
static endswith($hay, $needle)
Definition: UString.php:16
static isBoolean($value)
Definition: UString.php:43
static startswith($hay, $needle)
Definition: UString.php:12