10 const TRANSFORM_HTMLCHARS =
'htmlchars';
11 const TRANSFORM_LCFIRST =
'lcfirst';
12 const TRANSFORM_LOWER =
'lower';
13 const TRANSFORM_NL2BR =
'nl2br';
14 const TRANSFORM_NL2P =
'nl2p';
15 const TRANSFORM_NL2PBR =
'nl2pbr';
16 const TRANSFORM_TRIM =
'trim';
17 const TRANSFORM_UCFIRST =
'ucfirst';
18 const TRANSFORM_UCWORDS =
'ucwords';
19 const TRANSFORM_UPPER =
'upper';
20 const TRANSFORM_URLENCODE =
'urlencode';
21 const TRANSFORM_URLIFY =
'urlify';
23 const ATTRIBUTES_PATTERN =
'\\s+(?<key>[\\w\\-]+)(?<value>=(\'([^\']*)\'|"([^"]*)"|(\\w+)))?';
25 const PASSWORD_CHARS =
'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
26 const MAX_TIME = 20000000000;
33 public static function bool($value){
34 return $value ?
'true' :
'false';
46 public static function random($length = 32,$chars = null,$time = null){
47 switch($chars ?:
'*'){
48 case '*': $chars =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
49 case '+': $chars =
'0123456789abcdefghijklmnopqrstuvwxyz';
break;
50 case '@': $chars =
'23456789abcdefghjklmnpqrstuvwxyz';
break;
52 if(preg_match_all(
'/\\[(.)\\-(.)\\]/',$chars,$matches,PREG_SET_ORDER))
53 foreach($matches as list($full,$min,$max)) $chars = str_replace($full,implode(range($min,$max)),$chars);
55 $max = strlen($chars) - 1;
58 if(($time_length = ceil(log(self::MAX_TIME,$max))) > $length)
59 throw new \Exception(
"Not enough characters to encode time (need $time_length, have $length)");
60 $length -= $time_length;
61 if($time ===
true) $time = time();
62 while($time_length--){
63 $result = $chars[$time % $max] . $result;
64 $time = floor($time / $max);
67 while($length--) $result .= $chars[random_int(0,$max)];
76 $animals = require(__DIR__ .
'/../../data/animals.php');
77 $letter = strtolower(substr($animal = $animals[$index % count($animals)],0,1));
78 $adjectives = array_values(array_filter(require(__DIR__ .
'/../../data/adjectives.php'),
function($adjective) use ($letter){
79 return strtolower(substr($adjective,0,1)) == $letter;
81 return $adjectives[$index % count($adjectives)] .
' ' . $animal;
89 return $str ===
'' ? null : $str;
97 return str_replace([
"'",
'"'],
'',iconv(
'UTF-8',
'ASCII//TRANSLIT//IGNORE',$str));
105 public static function urlify($str,$replace =
'-'){
106 return strtolower(trim(preg_replace(
'/\\W+/',$replace,self::normalize($str)),$replace));
114 return strtr($str,require(__DIR__ .
'/../../data/emoji.php'));
123 public static function unleet($str,$upper =
false){
124 $leet = require(__DIR__ .
'/../../data/leet.php');
126 foreach($leet as $chars) $length = max($length,max(array_map(
'strlen',$chars)));
127 do foreach($leet as $char => $chars) $str = str_replace(
128 array_filter($chars,
function($char) use ($length){
129 return strlen($char) == $length;
131 $upper ? $char : strtolower($char),
145 return strcmp(mb_strtolower($str1),mb_strtolower($str2));
155 public static function pad($str,$length,$pad =
'0',$type = STR_PAD_LEFT){
156 return str_pad($str,$length + strlen($str) - mb_strlen($str),$pad,$type);
167 public static function format($str,$format,$pad = null,$type = STR_PAD_LEFT,$replace =
'*'){
168 if($pad !== null) $str = str_pad($str,substr_count($format,$replace),$pad,$type);
170 foreach(str_split($format) as $format)
if($format === $replace){
171 if($str ===
'')
break;
172 $result .= mb_substr($str,0,1);
173 $str = mb_substr($str,1);
175 else $result .= $format;
184 public static function camel($str,$delimiters =
' -_'){
185 return str_replace(str_split($delimiters),
'',ucwords($str,$delimiters));
193 public static function snake($str,$delimiter =
'_'){
194 return strtolower(preg_replace(
'/([A-Z])([A-Z][a-z])/',
"\$1$delimiter\$2",preg_replace(
'/([a-z\\d])([A-Z])/',
"\$1$delimiter\$2",$str)));
204 public static function limit($str,$length,$words =
false,$delimiter =
'…'){
205 if(mb_strlen($str) > $length){
206 $str = mb_substr($str,0,$length - mb_strlen($delimiter));
207 if($words && ($i = strrpos($str,
' '))) $str = substr($str,0,$i + 1);
219 parse_str($str,$data);
228 public static function strip(&$haystack,$needle){
229 $result = substr($haystack,0,$i = strpos($haystack . $needle,$needle));
230 $haystack = substr($haystack,$i + 1);
239 public static function pop(&$haystack,$needle){
240 $i = strrpos($haystack,$needle);
241 if($i ===
false)
return false;
242 $result = substr($haystack,$i + 1);
243 $haystack = substr($haystack,0,$i);
253 public static function part($haystack,$needle,$index = 0){
254 $parts = explode($needle,$haystack);
255 return $index < count($parts) ? $parts[$index] :
false;
265 public static function insert($str,$insert,$position,$length = 0){
266 return substr($str,0,$position) . $insert . substr($str,$position + $length);
276 return in_array($quote = substr($str,0,1),$quotes) && (substr($str,-1) == $quote) ? substr($str,1,-1) : $str;
284 public static function wrap($str,$width){
286 foreach(explode(
"\n",strtr($str,[
"\r" =>
'',
"\t" =>
' '])) as $line)
while($line){
287 if(($length = mb_strlen($line)) > $width){
288 preg_match(
"/^.{0,$width}\\s/u",$line,$space);
289 preg_match(
'/^.{0,' . ($width - 1) .
'}-/u',$line,$hyphen);
290 $length = ($space || $hyphen) ? max($space ? mb_strlen($space[0]) : 0,$hyphen ? mb_strlen($hyphen[0]) : 0) : $width;
292 $lines[] = rtrim(mb_substr($line,0,$length));
293 $line = mb_substr($line,$length);
302 public static function nl2p($str){
303 return ($str = trim($str)) ?
'<p>' . preg_replace(
'/\\s*\\n\\s*/',
'</p><p>',str_replace(
"\r",
'',$str)) .
'</p>' : null;
311 return ($str = trim($str)) ?
'<p>' . nl2br(preg_replace(
'/\\s*\\n\\s*\\n\\s*/',
'</p><p>',str_replace(
"\r",
'',$str))) .
'</p>' : null;
320 public static function transform($str,$methods,&$count = null){
321 if(!is_array($methods)) $methods = [$methods];
322 $count = count($methods);
323 foreach($methods as $method)
switch($method){
324 case self::TRANSFORM_HTMLCHARS: $str = htmlspecialchars($str);
break;
325 case self::TRANSFORM_LCFIRST: $str = lcfirst($str);
break;
326 case self::TRANSFORM_LOWER: $str = strtolower($str);
break;
327 case self::TRANSFORM_NL2BR: $str = nl2br($str);
break;
328 case self::TRANSFORM_NL2P: $str = self::nl2p($str);
break;
329 case self::TRANSFORM_NL2PBR: $str = self::nl2pbr($str);
break;
330 case self::TRANSFORM_TRIM: $str = trim($str);
break;
331 case self::TRANSFORM_UCFIRST: $str = ucfirst($str);
break;
332 case self::TRANSFORM_UCWORDS: $str = ucwords($str);
break;
333 case self::TRANSFORM_UPPER: $str = strtoupper($str);
break;
334 case self::TRANSFORM_URLENCODE: $str = rawurlencode($str);
break;
335 case self::TRANSFORM_URLIFY: $str = self::urlify($str);
break;
338 if($i = strpos($method,
'=')) $str = str_replace(substr($method,0,$i),substr($method,$i + 1),$str,$replaced);
339 if(!$replaced) $count--;
350 return substr($haystack,0,strlen($needle)) == $needle;
359 return self::startsWith($haystack,$needle) ? $haystack : $needle . $haystack;
367 public static function endsWith($haystack,$needle){
368 return substr($haystack,-strlen($needle)) == $needle;
376 public static function endWith($haystack,$needle){
377 return self::endsWith($haystack,$needle) ? $haystack : $haystack . $needle;
386 return is_string($value) && preg_match(
'/^(\\-?[1-9]\\d*|0)(\\.\\d+)?$/',$value);
401 public static function operator($ref,$operator,$value,$default =
false){
403 case '==':
return $ref == $value;
404 case '!=':
return $ref != $value;
405 case '>':
return $ref > $value;
406 case '>=':
return $ref >= $value;
407 case '<':
return $ref < $value;
408 case '<=':
return $ref <= $value;
409 case '%':
return $ref % $value;
410 case '=%':
return $ref % $value == 0;
411 case '*-':
return self::startsWith($ref,$value);
412 case '-*':
return self::endsWith($ref,$value);
413 case '*':
return strpos($ref,$value) !==
false;
414 case '//':
return preg_match(substr($value,0,1) ==
'/' ? $value :
'/' . $value .
'/',$ref);
425 public static function implode($array,$last_delimiter = null,$delimiter =
', '){
426 $last = array_pop($array);
427 return $array ? implode($delimiter,$array) . ($last_delimiter ?: $delimiter) . $last : $last;
437 public static function ranges($array,$delimiter =
', ',$separator =
' - '){
439 foreach(Record::ranges($array) as $start => $end)
440 if($end > $start + 1) $result[] = $start . $separator . $end;
443 if($start != $end) $result[] = $end;
445 return implode($delimiter,$result);
455 public static function replaceDate($str,$time = null,$open =
'[',$close =
']'){
456 if(preg_match_all(
'/' . preg_quote($open,
'/') .
'(\w+)' . preg_quote($close,
'/') .
'/',$str,$matches,PREG_SET_ORDER)){
457 if(!$time) $time = time();
458 foreach($matches as $match) $str = str_replace($match[0],date($match[1],$time),$str);
469 if($str && preg_match_all(
'/' . self::ATTRIBUTES_PATTERN .
'/',$str,$matches,PREG_SET_ORDER))
foreach($matches as $match)
470 $attribs[$match[
'key']] = array_key_exists(
'value',$match) ? html_entity_decode(array_pop($match)) :
true;
static emojify($str)
Replace ASCII emoji's by their Unicode equivalent.
static random($length=32, $chars=null, $time=null)
Generate a random string.
static parse($str)
Parse a string into variables.
static urlify($str, $replace='-')
Transform a string to a URL-friendly version.
static startsWith($haystack, $needle)
Check if a string starts with a specific value.
static strip(&$haystack, $needle)
Return part of a string until a needle.
static nl2p($str)
Replace all newlines with HTML paragraph tags.
static nl2pbr($str)
Replace all multi newlines with HTML paragraph tags, and single newlines with line breaks...
static limit($str, $length, $words=false, $delimiter='…')
Limit a string to a number of characters.
static codeName($index)
Generate a code-name, based on the index.
static startWith($haystack, $needle)
Make sure a string starts with a specific value.
static part($haystack, $needle, $index=0)
Return part of a string until a needle.
static ranges($array, $delimiter=', ', $separator=' - ')
Show ranges from a numerical array.
static nullify($str)
Transform an empty string to a null.
static endsWith($haystack, $needle)
Check if a string ends with a specific value.
static bool($value)
Returns literally 'true' or 'false'.
static format($str, $format, $pad=null, $type=STR_PAD_LEFT, $replace=' *')
Format a string.
static normalize($str)
Remove accents from a string.
static pop(&$haystack, $needle)
Return part of a string after a needle.
static wrap($str, $width)
Wrap a string into multiple lines (better wordwrap()).
static endWith($haystack, $needle)
Make sure a string ends with a specific value.
static pad($str, $length, $pad='0', $type=STR_PAD_LEFT)
Pad function that defaults to left padding with zeros.
static operator($ref, $operator,$value, $default=false)
Evaluate an operator between a value and a reference value.
static stripQuotes($str, $quotes=["'",'"'])
Remove optinal quotes from a string.
static camel($str, $delimiters=' -_')
Converts a delimited string to CamelCase.
static snake($str, $delimiter='_')
Converts a CamelCased string to snake_case.
static transform($str, $methods, &$count=null)
Transforms a string according to a certain function.
static numeric($value)
Numeric detection.
static insert($str, $insert, $position, $length=0)
Insert a string at a certain position in another string.
static implode($array, $last_delimiter=null, $delimiter=', ')
Convert an array to a list.
static attributes($str)
Extract attributes from a string.
static unleet($str, $upper=false)
Remove leet-speak (1337 5p33k) from a string.
static replaceDate($str, $time=null, $open='[', $close=']')
Replace date tags in a string.
static icompare($str1, $str2)
Case insesitive string comparison.