wpdb::strip_invalid_text( array $data ): array|WP_Error
- Since
- 4.2.0
- Source
wp-includes/class-wpdb.php:3559
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
- Scaling
- Scales with input
- Instructions
- 7–34
- Plugin surface
- None
- Called by
- 3
Reaches the database via ->get_row().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 214.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7–8 | none |
| always | 29–33 | ->get_row(), array_keys() |
| always | 31–34 | ->get_row(), __() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 220 | 7–34 | 35 | 6 more instructions than PHP 8.5 |
| 8.5 | 214 | 7–34 | 34 | |
| 8.4 | 214 | 7–34 | 34 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 223 | 7–37 | 34 | |
| 8.2 | 223 | 7–37 | 34 | |
| 8.1 | 223 | 7–37 | 34 | |
| 7.4 | 223 | 7–37 | 34 |
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
- mbstring_binary_safe_encoding()Sets the mbstring internal encoding to a binary safe encoding when func_overload is enabled.
- reset_mbstring_encoding()Resets the mbstring internal encoding to a users previously set encoding.
- mb_strlen()Compat function to mimic mb_strlen().
- mb_substr()Compat function to mimic mb_substr().
- __()Retrieves the translation of $text.
- wpdb::check_ascii()Checks if a string is ASCII.
- wpdb::prepare()Prepares a SQL query for safe execution.
- wpdb::get_row()Retrieves one row from the database.
- WP_Error::__construct()Initializes the error.
Used by · 3
- wpdb::process_fields()Processes arrays of field/value pairs and field formats.
- wpdb::strip_invalid_text_for_column()Strips any invalid characters from the string for a given table and column.
- wpdb::strip_invalid_text_from_query()Strips any invalid characters from the query.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.