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): ClosureWhen 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): stringThis 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']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 redIf 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']