wpdb::strip_invalid_text_from_query() WordPress Method

The wpdb::strip_invalid_text_from_query() method is used to remove invalid SQL characters from a query. This is useful when you want to make sure that your query does not contain any invalid characters that could cause an error.

wpdb::strip_invalid_text_from_query( string $query ) #

Strips any invalid characters from the query.


Parameters

$query

(string)(Required)Query to convert.


Top ↑

Return

(string|WP_Error) The converted query, or a WP_Error object if the conversion fails.


Top ↑

Source

File: wp-includes/wp-db.php

	protected function strip_invalid_text_from_query( $query ) {
		// We don't need to check the collation for queries that don't read data.
		$trimmed_query = ltrim( $query, "\r\n\t (" );
		if ( preg_match( '/^(?:SHOW|DESCRIBE|DESC|EXPLAIN|CREATE)\s/i', $trimmed_query ) ) {
			return $query;
		}

		$table = $this->get_table_from_query( $query );
		if ( $table ) {
			$charset = $this->get_table_charset( $table );
			if ( is_wp_error( $charset ) ) {
				return $charset;
			}

			// We can't reliably strip text from tables containing binary/blob columns.
			if ( 'binary' === $charset ) {
				return $query;
			}
		} else {
			$charset = $this->charset;
		}

		$data = array(
			'value'   => $query,
			'charset' => $charset,
			'ascii'   => false,
			'length'  => false,
		);

		$data = $this->strip_invalid_text( array( $data ) );
		if ( is_wp_error( $data ) ) {
			return $data;
		}

		return $data[0]['value'];
	}


Top ↑

Changelog

Changelog
VersionDescription
4.2.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.