Skip to content

array_strip_tags

Tonya Mork edited this page Mar 12, 2017 · 3 revisions

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:

  1. This function also trims each element's value after processing the strip_tags function.
  2. The original subject array is changed when running this function, as it's passed by reference.

Syntax

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.

When to Use

Use this function when you need to clean up an array of attributes before rendering out to the browser or storing in the database.

For Example

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,
);

Single Depth

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,
);

Go Deeper

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.


« Back to Array API

Clone this wiki locally