Skip to content

Latest commit

 

History

History
287 lines (198 loc) · 5.07 KB

string.md

File metadata and controls

287 lines (198 loc) · 5.07 KB

string

This file contains some useful String functions.

  • split - Curried version of explode.

  • join - Curried version of implode.

  • replace - Curried version of str_replace.

  • regReplace - Curried version of preg_replace.

  • upperCase - Alias of strtoupper.

  • lowerCase - Alias of strtolower.

  • camelCase - Gets the camlCase version of a string.

  • snakeCase - Gets the snake-case of the string using $delimiter as separator.

  • startsWith - Checks if $string starts with $token.

  • endsWith - Checks if $string ends with $token.

  • test - Checks if a string matches a regular expression.

  • match - Performs a global regular expression match and returns array of results.

  • occurences - Curried version of substr_count with changed order of parameters,

  • chunks - Splits a string into chunks without spliting any group surrounded with some specified characters.

split

split(string $delimiter, string $string) : array
String -> String -> [String]

Curried version of explode.

$words = F\split(' ');
$words('Hello World'); //=> ['Hello', 'World']

join

join(string $glue, array $pieces) : string
String -> [String] -> String

Curried version of implode.

$sentence = F\join(' ');
$sentence(['Hello', 'World']); //=> 'Hello World'

replace

replace(string $search, string $replacement, string $string) : string
String|[String] -> String|[String] -> String -> String

Curried version of str_replace.

$string = 'a b c d e f';
$noSpace = F\replace(' ', '');
$noSpace($string); //=> 'abcdef'
F\replace(['a', 'b', ' '], '', $string); //=> 'cdef'
F\replace(['a', 'e', ' '], ['x', 'y', ''], $string); //=> 'xbcdyf'

regReplace

regReplace(string $pattern, string $replacement, string $string) : string
String -> String -> String -> String

Curried version of preg_replace.

$string = 'A12;b_{F}|d';
$alpha = F\regReplace('/[^a-z]+/i', '');
$alpha($string); //=> 'AbFd'

upperCase

upperCase(string $string) : string
String -> String

Alias of strtoupper.

F\upperCase('hello'); //=> 'HELLO'

lowerCase

lowerCase(string $string) : string
String -> String

Alias of strtolower.

F\lowerCase('HeLLO'); //=> 'hello'

camelCase

camelCase(string $string) : string
String -> String

Gets the camlCase version of a string.

F\camelCase('Yes, we can! 123'); //=> 'yesWeCan123'

snakeCase

snakeCase(string $delimiter, string $string) : string
String -> String -> String

Gets the snake-case of the string using $delimiter as separator.

$underscoreCase = F\snakeCase('_');
$underscoreCase('IAm-Happy'); //=> 'i_am_happy'

startsWith

startsWith(string $token, string $string) : bool
String -> String -> Boolean

Checks if $string starts with $token.

$http = F\startsWith('https://');
$http('https://gitbub.com'); //=> true
$http('gitbub.com'); //=> false

endsWith

endsWith(string $token, string $string) : bool
String -> String -> Boolean

Checks if $string ends with $token.

$dotCom = F\endsWith('.com');
$dotCom('https://gitbub.com'); //=> true
$dotCom('php.net'); //=> false

test

test(string $pattern, string $string) : bool
String -> String -> Boolean

Checks if a string matches a regular expression.

$numeric = F\test('/^[0-9.]+$/');
$numeric('123.43'); //=> true
$numeric('12a3.43'); //=> false

match

match(string $pattern, string $string) : array
String -> String -> [String]

Performs a global regular expression match and returns array of results.

$numbers = F\match('/[0-9.]+/');
$numbers('Hello World'); //=> []
$numbers('12 is 4 times 3'); //=> ['12', '4', '3']

occurences

occurences(string $token, string $text) : int
String -> String -> Number

Curried version of substr_count with changed order of parameters,

$spaces = F\occurences(' ');
$spaces('Hello'); //=> 0
$spaces('12 is 4 times 3'); //=> 4

chunks

chunks(string $surrounders, string $separator, sring $text) : array
String -> String -> String -> [String]

Splits a string into chunks without spliting any group surrounded with some specified characters.

$surrounders is a string where each pair of characters specifies the starting and ending characters of a group that should not be split.

Note that this function assumes that the given $text is well formatted

$names = F\chunks("''()\"\"", ' ');
$names('Foo "Bar Baz" (Some other name)'); //=> ['Foo', '"Bar Baz"', '(Some other name)']
$names("This 'Quote\'s Test' is working"); //=> ['This', "'Quote\'s Test'", 'is', 'working']

$groups = F\chunks('(){}', '->');
$groups('1->2->(3->4->5)->{6->(7->8)}->9'); //=> ['1', '2', '(3->4->5)', '{6->(7->8)}', '9']