sanitize_email( string $email ): string
- Since
- 1.5.0
- Source
wp-includes/formatting.php:3831
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
- Scaling
- Scales with input
- Instructions
- 11–56
- Plugin surface
- 1 hook
- Called by
- 4
Touches nothing outside its own arguments.
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 115.
Third-party callbacks on 'sanitize_email' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 11–15 | apply_filters() |
| always | 28–37 | explode(), apply_filters() |
$local !== "" && $domain !== "" | 45–56 | explode(), explode(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 115 | 11–56 | 10 | |
| 8.5 | 115 | 11–56 | 10 | |
| 8.4 | 115 | 11–56 | 10 | 21 fewer instructions than PHP 8.3 |
| 8.3 | 136 | 11–71 | 10 | |
| 8.2 | 136 | 11–71 | 10 | |
| 8.1 | 136 | 11–71 | 10 | |
| 7.4 | 136 | 11–71 | 10 |
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:
- apply_filters( sanitize_email )filterline 3847 (+16 into the body)
Filters a sanitized email address.
- apply_filters( sanitize_email )filterline 3853 (+22 into the body)
Filters a sanitized email address.
- apply_filters( sanitize_email )filterline 3866 (+35 into the body)
Filters a sanitized email address.
- apply_filters( sanitize_email )filterline 3876 (+45 into the body)
Filters a sanitized email address.
- apply_filters( sanitize_email )filterline 3883 (+52 into the body)
Filters a sanitized email address.
- apply_filters( sanitize_email )filterline 3892 (+61 into the body)
Filters a sanitized email address.
- apply_filters( sanitize_email )filterline 3915 (+84 into the body)
Filters a sanitized email address.
- apply_filters( sanitize_email )filterline 3926 (+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
- sanitize_option()Sanitizes various option values based on the nature of the option.
- wp_create_user_request()Creates and logs a user request to perform a specific action.
- wpmu_signup_user()Records user signup information for future activation.
- wpmu_validate_user_signup()Sanitizes and validates data required for a user sign-up.
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.
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/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.