'PHP: using strip_tags with parameters with array_map
I've looked at other posts but can't seem to get to a working solution in place to the point where I am now looking at a blank canvas.
$l = array_map( 'strip_tags', $l );
This is what I currently have but I would like to keep <p>, <a>, <ul> and <li> tags
Solution 1:[1]
If $l is an array,
First of all, avoid to affect to the variable the output of a function with which you pass the variable itself
you can write something like this:
$output = array_map(function($item) {
return strip_tags($item, '<p><a><ul><li>');
}, $l)
Solution 2:[2]
To add to JessGabriel's answer:
Starting in PHP 8.1, you can't pass NULLs to internal function parameters (https://php.watch/versions/8.1/internal-func-non-nullable-null-deprecation). So for strip_tags, I have a quick and dirty if statement so it doesn't toss an error:
$output= array_map(function($v){
if (is_null($v)) {
$v = '';
}
return trim(strip_tags($v));
}, $myArray);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JessGabriel |
| Solution 2 | TechJunkie |
