-
Notifications
You must be signed in to change notification settings - Fork 1
array_strip_tags
The array_strip_tags()
function strips the HTML and PHP tags from each element's string value. The function iterates through the subject array and then uses strip_tags()
to each string value.
You can escape one level of the array, or for deeper arrays, you can set the second argument to true.
Notes:
- This function also trims each element's value after processing the
strip_tags
function. - The original subject array is changed when running this function, as it's passed by reference.
array array_esc_attr(
array &$subjectArray,
[ bool $goDeep = false ]
);
Where:
$subjectArray
is the array to work on
$goDeep
when true
, this function will escape all string values at each level within the subject array; else, only the first depth level is escaped.
Use this function when you need to clean up an array of attributes before rendering out to the browser or storing in the database.
Let's use this given dataset for our examples:
$dataSet = array(
'user_id' => 101,
'name' => ' Tonya ',
'email' => ' <a:mailto="[email protected]">[email protected] </a> ',
'social' => array(
'twitter' => '<a href="https://twitter.com/hellofromtonya">@hellofromtonya </a> ',
'website' => '<a href="https://foo.com">https://foo.com </a> ',
),
'has_access' => true,
);
Let's clean up the first depth level of the array:
array_strip_tags( $dataSet );
Running this code changes the $dataSet
array to:
array(
'user_id' => 101,
'name' => 'Tonya',
'email' => '[email protected]',
'social' => array(
'twitter' => '<a href="https://twitter.com/hellofromtonya">@hellofromtonya </a> ',
'website' => '<a href="https://foo.com">https://foo.com </a> ',
),
'has_access' => true,
);
Let's clean the entire array by specifying the second argument as true
:
array_strip_tags( $dataSet, true );
Running this code changes the $dataSet
array to:
array(
'user_id' => 101,
'name' => 'Tonya',
'email' => '[email protected]',
'social' => array(
'twitter' => '@hellofromtonya',
'website' => 'https://foo.com',
),
'has_access' => true,
);
Notice that the HTML has been stripped off and all of the whitespace has been removed.