Strings\addSlashes()

transformer returns Closure pure
string → (string → string)
At a glance — Bind the chars to escape; the returned Closure prefixes each of them with a backslash in any input. Wraps addcslashes().

Creates a function which allows for adding slashes to a string. The created function can then reused over any string, or used as part of a Higher Order Function such as array_map().

/**
  * @param string $charList The Char list to add slashes too.
  * @return Closure(string):string
  */
Strings\addSlashes(string $charList): Closure

Returned Closure

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

/**
 * @param string $string The string to have char, slash escaped.
 * @return string
 */
$function (string $string): string

Examples

Partial Application

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

// Create the closure that add slashes to any A or B.
$format = Strings\addSlashes('ap');

// Called as a function.
echo $format('This is an example'); /// This is \an ex\am\ple

// Used in a higher order function.  
$array = array_map( $format, ['This is an example', 'Another example'] );
 
print_r($array); // ['This is \an ex\am\ple', 'Another ex\am\ple']

Curried

This can be called inline using currying.

// With decimal full stop and comma as thousands separator.
echo Strings\addSlashes('al')('Apples are red'); // App\les \are red

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\addSlashes('ap'), ['This is an example', 'Another example'] );
print_r($array); // ['This is \an ex\am\ple', 'Another ex\am\ple']

Details

Strings Functions

Releated String manipulation Functions