Strings\lastChar()

transformer returns Closure pure
string → (string → string)
At a glance — Slice from the last occurrence of a character. Wraps strrchr().

Returns a function which can be used to get the contents of a string before (and including) the occurrence of a defined char. The created function can then reused over any string, or used as part of a Higher Order Function such as array_map().

/**
  *  @param string $chars All chars to check with.
  * @return Closure(string):strin
  */
Strings\lastChar(string $chars): Closure

Returned Closure

When Strings\lastChar() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param string $haystack
  * @return string
  * @psalm-pure
  */ 
$function(string $haystack): bool

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

// Create function to get the rest of a string after the first occurrence of either m or i
$firstMorI = Strings\lastChar('mi');

// Called as a function.
$firstMorI('This is a Simple text.'); // 'mple text'

// Used in a higher order function.
$array = array_map($firstMorI, ['This is a Simple text', 'That may be a simple text']);
print_r($array); // ['mple text', 'mple text']

Curried

This can be called inline using currying.

print Strings\lastChar('a')('This is a Simple text'); // 'mple text'

Inlined with Higher Order Function

If you are not planning on reusing the Closure created, you can just call it inline with a higher order function as its callable.

$array = array_map(Strings\lastChar('a'), ['This is a Simple text.', 'That may be a simple text.']);
print_r($array); // ['mple text', 'mple text']

Details

Strings Functions

Releated String manipulation Functions