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 154 155 156 157 158 159 160 161 162 163 164 165 166
<?php namespace amekusa\plz; main::required;
/**
* String utilities
*
* To get started, place the following line around top of your code.
* ```php
* use amekusa\plz\str;
* ```
*/
abstract class str {
/**
* Returns whether `$X` equals `$Y`
* @example Demonstration
* ```php
* $var1 = 'ABC';
* $var2 = 'ABC';
* $var3 = 'Abc';
* var_dump( str::eq($var1, $var2) );
* var_dump( str::eq($var1, $var3) );
* var_dump( str::eq($var1, $var3, true) ); // Case-insensitive
* ```
* ```php
* bool(true)
* bool(false)
* bool(true)
* ```
* @param string $X A string to compare with `$Y`
* @param string $Y A string to compare with `$X`
* @param boolean $CaseInsensitive *(optional)* Whether or not to ignore letter case
* @return boolean
*/
static function eq($X, $Y, $CaseInsensitive = false) {
if ($CaseInsensitive) return strcasecmp($X, $Y) === 0;
return $X === $Y;
}
/**
* Returns whether `$X` contains any visible character
* @example Demonstration
* ```php
* $var1 = " \t \n "; // Spaces, Tab, Linebreak
* $var2 = " \t \n _ "; // Spaces, Tab, Linebreak, and Underscore
* var_dump( str::is_visible($var1) );
* var_dump( str::is_visible($var2) );
* ```
* ```php
* bool(false)
* bool(true)
* ```
* @param string $X
* @return boolean
*/
static function is_visible($X) {
return !empty(str::trim($X));
}
/**
* Returns whether `$X` contains any multibyte character
* @example Demonstration
* ```php
* $var1 = "ABC 123";
* $var2 = "ABC 一二三";
* var_dump( str::is_mb($var1) );
* var_dump( str::is_mb($var2) );
* ```
* ```php
* bool(false)
* bool(true)
* ```
* @param string $X
* @return boolean
*/
static function is_mb($X) {
return mb_strlen($X, mb_internal_encoding()) < strlen($X);
}
/**
* Returns whether `$X` contains `$Y`
* @example Demonstration
* ```php
* $haystack = "ABCDEFGHI";
* $needle1 = "DEF";
* $needle2 = "Def";
* var_dump( str::contains($haystack, $needle1) );
* var_dump( str::contains($haystack, $needle2) );
* var_dump( str::contains($haystack, $needle2, true) ); // Case-insensitive
* ```
* ```php
* bool(true)
* bool(false)
* bool(true)
* ```
* @param string $X A haystack
* @param string $Y A needle
* @param boolean $CaseInsensitive *(optional)* Whether or not to ignore letter case
* @return boolean
*/
static function contains($X, $Y, $CaseInsensitive = false) {
return $CaseInsensitive ? (stripos($X, $Y) !== false) : (strpos($X, $Y) !== false);
}
/**
* Removes control characters in `$X`
* @param string $X
* @return string A processed string
*/
static function trim($X) {
static $mask = "[\\x0-\x20\x7f\xc2\xa0\xe3\x80\x80]";
return preg_replace("/\A{$mask}++|{$mask}++\z/u", '', $X);
}
/**
* Replaces newline characters in `$X` with the supplied string
* @example Replacing every line-breaks with `<br>\n`
* ```php
* $var = <<<TEXT
* If the doors of perception were cleansed
* every thing would appear to man as it is,
* Infinite.
* TEXT;
* echo str::replace_nl($var, "<br>\n");
* ```
* ```html
* If the doors of perception were cleansed<br>
* every thing would appear to man as it is,<br>
* Infinite.
* ```
* @param string $X A haystack
* @param string $With *(optional)* A replacement string
* @return string A processed string
*/
static function replace_nl($X, $With = '') {
static $needles = array ("\r\n", "\r", "\n");
return str_replace($needles, $With, $X);
}
/**
* Repeats `$X`
* @example Demonstration
* ```php
* echo str::repeat('Knock');
* ```
* ```php
* KnockKnock
* ```
* @example Repeating so many times
* ```php
* echo 'YEA'.str::repeat('H', 32).'!!';
* ```
* ```php
* YEAHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH!!
* ```
* @param string $X A string to repeat
* @param integer $Times *(optional)* How many times `$X` to appear
* @param string $Insert *(optional)* A string to insert after every repetition
*/
static function repeat($X, $Times = 2, $Insert = '') {
if ($Times == 1) return $X;
$r = '';
for ($i = 1; $i < $Times; $i++) $r .= ($X . $Insert);
return $r.$X;
}
}