wppaste
WordPress

wpdb::strip_invalid_text( array $data ): array|WP_Error

Since
4.2.0
Source
wp-includes/class-wpdb.php:3624
Strips any invalid characters based on value/charset pairs.

Compatibility

WordPress
since 4.2.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$dataarray
Array of value arrays. Each value array has the keys 'value', 'charset', and 'length'.
An optional 'ascii' key can be set to false to avoid redundant ASCII checks.

Return value

array|WP_Error
The $data parameter, with invalid characters removed from each value.
This works as a passthrough: any additional keys such as 'field' are retained in each value array. If we cannot remove invalid characters, a WP_Error object is returned.

Performance profile

How much work a call to wpdb::strip_invalid_text() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_row().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
7–34

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 214.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
3

3 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • sqldatabase query->get_row()called directly

Further down the call graph this can also reach option, hook, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wpdb::strip_invalid_text() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always7–8none
always29–33->get_row(), array_keys()
always31–34->get_row(), __()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev2207–34356 more instructions than PHP 8.5
8.52147–3434
8.42147–34349 fewer instructions than PHP 8.3
8.32237–3734
8.22237–3734
8.12237–3734
7.42237–3734

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 9

Used by · 3

Source code

	protected function strip_invalid_text( $data ) {		$db_check_string = false; 		foreach ( $data as &$value ) {			$charset = $value['charset']; 			if ( is_array( $value['length'] ) ) {				$length                  = $value['length']['length'];				$truncate_by_byte_length = 'byte' === $value['length']['type'];			} else {				$length = false;				/*				 * Since we have no length, we'll never truncate. Initialize the variable to false.				 * True would take us through an unnecessary (for this case) codepath below.				 */				$truncate_by_byte_length = false;			} 			// There's no charset to work with.			if ( false === $charset ) {				continue;			} 			// Column isn't a string.			if ( ! is_string( $value['value'] ) ) {				continue;			} 			$needs_validation = true;			if (				// latin1 can store any byte sequence.				'latin1' === $charset			||				// ASCII is always OK.				( ! isset( $value['ascii'] ) && $this->check_ascii( $value['value'] ) )			) {				$truncate_by_byte_length = true;				$needs_validation        = false;			} 			if ( $truncate_by_byte_length ) {				mbstring_binary_safe_encoding();				if ( false !== $length && strlen( $value['value'] ) > $length ) {					$value['value'] = substr( $value['value'], 0, $length );				}				reset_mbstring_encoding(); 				if ( ! $needs_validation ) {					continue;				}			} 			// utf8 can be handled by regex, which is a bunch faster than a DB lookup.			if ( ( 'utf8' === $charset || 'utf8mb3' === $charset || 'utf8mb4' === $charset ) && function_exists( 'mb_strlen' ) ) {				$regex = '/					(						(?: [\x00-\x7F]                  # single-byte sequences   0xxxxxxx						|   [\xC2-\xDF][\x80-\xBF]       # double-byte sequences   110xxxxx 10xxxxxx						|   \xE0[\xA0-\xBF][\x80-\xBF]   # triple-byte sequences   1110xxxx 10xxxxxx * 2						|   [\xE1-\xEC][\x80-\xBF]{2}						|   \xED[\x80-\x9F][\x80-\xBF]						|   [\xEE-\xEF][\x80-\xBF]{2}'; 				if ( 'utf8mb4' === $charset ) {					$regex .= '						|    \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences   11110xxx 10xxxxxx * 3						|    [\xF1-\xF3][\x80-\xBF]{3}						|    \xF4[\x80-\x8F][\x80-\xBF]{2}					';				} 				$regex         .= '){1,40}                          # ...one or more times					)					| .                                  # anything else					/x';				$value['value'] = preg_replace( $regex, '$1', $value['value'] ); 				if ( false !== $length && mb_strlen( $value['value'], 'UTF-8' ) > $length ) {					$value['value'] = mb_substr( $value['value'], 0, $length, 'UTF-8' );				}

Changelog

Introduced in 4.2.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wpdb.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.