Strings\stripTags()

transformer returns Closure pure
(string | string[])? → (string → string)
At a glance — Wraps strip_tags(). Bind the allow-list once, reuse.

Allows you to create a function which can be used to strip tags from a string. Optional section of allowed tags can be defined. This can either be used as part of a Higher Order Function such as array_map() or as part of a compiled/pipe function.

/**
  * @param string|string[]|null $allowedTags The allowed tags, pass null or leave blank for none.
* @return Closure(string):string
  */
 Strings\stripTags($allowedTags = null): Closure

Returned Closure

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

/**
  * @param string $string The string to strip tags from.
  * @return string
  * @psalm-pure
  */ 
$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 a function that strips all tags from a string except <p> and <a>
$stripTags = Strings\stripTags(['p', 'a']); 
// OR  
$stripTags = Strings\stripTags('<p><a>'); 

// Called as a function.
echo $stripTags('<p><span>Hello</span></p> <a href="#"><span>World</span></a>'); 
// <p>Hello</p> <a href="#">World</a>

// Used in a higher order function.
$array = array_map( $stripTags, [
   '<p><span>Hello</span></p> <a href="#"><span>World</span></a>'
   '<div><h1><a href="#">Hello</a></h1></div>'
]);

 print_r($array); // [<p>Hello</p> <a href="#">World</a>, <a href="#">Hello</a>]

Curried

This can be called inline using currying.

echo Strings\stripTags('<p><a>')('<p><span>Hello</span></p> <a href="#"><span>World</span></a>'); 
// <p>Hello</p> <a href="#">World</a>

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\stripTags(['p', 'a']), [
   '<p><span>Hello</span></p> <a href="#"><span>World</span></a>'
   '<div><h1><a href="#">Hello</a></h1></div>'
]);
print_r($array); // [<p>Hello</p> <a href="#">World</a>, <a href="#">Hello</a>]

Details

Strings Functions

Releated String manipulation Functions