wppaste
WordPress

sanitize_email( string $email ): string

Since
1.5.0
Source
wp-includes/formatting.php:3760
Strips out all characters that are not allowable in an email.

Compatibility

WordPress
since 1.5.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

$emailstring
Email address to filter.

Return value

string
Filtered email address.

Performance profile

How much work a call to sanitize_email() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
11–56

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

Plugin surface
1 hook

Third-party callbacks on 'sanitize_email' run inside this call, and their cost is not bounded by anything here.

Called by
4

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

What it touches

  • hookthird-party callbacksapply_filters()called directly

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 sanitize_email() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always11–15apply_filters()
always28–37explode(), apply_filters()
$local !== "" && $domain !== ""45–56explode(), explode(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11511–5610
8.511511–5610
8.411511–561021 fewer instructions than PHP 8.3
8.313611–7110
8.213611–7110
8.113611–7110
7.413611–7110

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.

Hooks and filters fired · 8

8 hooks fire while sanitize_email() runs, in this order:

  1. apply_filters( sanitize_email )filterline 3776 (+16 into the body)

    Filters a sanitized email address.

  2. apply_filters( sanitize_email )filterline 3782 (+22 into the body)

    Filters a sanitized email address.

  3. apply_filters( sanitize_email )filterline 3795 (+35 into the body)

    Filters a sanitized email address.

  4. apply_filters( sanitize_email )filterline 3805 (+45 into the body)

    Filters a sanitized email address.

  5. apply_filters( sanitize_email )filterline 3812 (+52 into the body)

    Filters a sanitized email address.

  6. apply_filters( sanitize_email )filterline 3821 (+61 into the body)

    Filters a sanitized email address.

  7. apply_filters( sanitize_email )filterline 3844 (+84 into the body)

    Filters a sanitized email address.

  8. apply_filters( sanitize_email )filterline 3855 (+95 into the body)

    Filters a sanitized email address.

Uses · 1

  • apply_filters()Calls the callback functions that have been added to a filter hook.

Used by · 4

Source code

function sanitize_email( $email ) {	// Test for the minimum length the email can be.	if ( strlen( $email ) < 6 ) {		/**		 * Filters a sanitized email address.		 *		 * This filter is evaluated under several contexts, including 'email_too_short',		 * 'email_no_at', 'local_invalid_chars', 'domain_period_sequence', 'domain_period_limits',		 * 'domain_no_periods', 'domain_no_valid_subs', or no context.		 *		 * @since 2.8.0		 *		 * @param string $sanitized_email The sanitized email address.		 * @param string $email           The email address, as provided to sanitize_email().		 * @param string|null $message    A message to pass to the user. null if email is sanitized.		 */		return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );	} 	// Test for an @ character after the first position.	if ( false === strpos( $email, '@', 1 ) ) {		/** This filter is documented in wp-includes/formatting.php */		return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );	} 	// Split out the local and domain parts.	list( $local, $domain ) = explode( '@', $email, 2 ); 	/*	 * LOCAL PART	 * Test for invalid characters.	 */	$local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );	if ( '' === $local ) {		/** This filter is documented in wp-includes/formatting.php */		return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );	} 	/*	 * DOMAIN PART	 * Test for sequences of periods.	 */	$domain = preg_replace( '/\.{2,}/', '', $domain );	if ( '' === $domain ) {		/** This filter is documented in wp-includes/formatting.php */		return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );	} 	// Test for leading and trailing periods and whitespace.	$domain = trim( $domain, " \t\n\r\0\x0B." );	if ( '' === $domain ) {		/** This filter is documented in wp-includes/formatting.php */		return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );	} 	// Split the domain into subs.	$subs = explode( '.', $domain ); 	// Assume the domain will have at least two subs.	if ( 2 > count( $subs ) ) {		/** This filter is documented in wp-includes/formatting.php */		return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );	} 	// Create an array that will contain valid subs.	$new_subs = array(); 	// Loop through each sub.	foreach ( $subs as $sub ) {		// Test for leading and trailing hyphens.		$sub = trim( $sub, " \t\n\r\0\x0B-" ); 		// Test for invalid characters.		$sub = preg_replace( '/[^a-z0-9-]+/i', '', $sub ); 		// If there's anything left, add it to the valid subs.		if ( '' !== $sub ) {			$new_subs[] = $sub;		}	}

Changelog

Introduced in 1.5.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.0.4 tag, from src/wp-includes/formatting.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.