antispambot( string $email_address, int $hex_encoding = 0 ): string
- Since
- 0.71, 7.1.0
- Source
wp-includes/formatting.php:2952
Description
Typically this will randomly replace characters from the email address with HTML character references; however, when the hex encoding parameter is set, some characters will also be represented in their percent-encoded form.
Because this function is randomized, the outputs for any given input may differ between calls. This helps diversify the ways the email addresses are obscured.
When non-UTF-8 inputs are provided, any spans of invalid UTF-8 bytes will be passed through without any obfuscation.
Example:
$email = '[email protected]';
$obscured = antispambot( $email );
$obscured === 'noreply@example.com';
// Hex-encoding also obscures characters with percent-encoding.
$obscured = antispambot( $email, 1 );
$obscured === '%6eore%70l%79@%65x%61mple%2e%63%6fm';
// Non-UTF-8 characters are not obfuscated. "\xFC" is Latin1 "ü".
$obscured = antispambot( "b\[email protected]" );
$obscured === 'b�cher@library.de';
$obscured === "b\xFCcher@library.de"Compatibility
- WordPress
- since 7.1.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
$email_addressstring- Email address.
$hex_encodingintoptional- Set to 1 to enable hex encoding.Default:
0
Return value
string- Converted email address.
Performance profile
How much work a call to antispambot() 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
- 12–24
- Plugin surface
- None
- Called by
- 1
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 70.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost antispambot() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$at | 12 | none |
$at && $invalid_length === 0 | 24 | _wp_scan_utf8() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 71 | 12–24 | 9 | 1 more instruction than PHP 8.5 |
| 8.5 | 70 | 12–24 | 9 | |
| 8.4 | 70 | 12–24 | 9 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 79 | 15–27 | 9 | |
| 8.2 | 79 | 15–27 | 9 | 1 more instruction than PHP 8.1 |
| 8.1 | 78 | 15–27 | 9 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 79 | 15–27 | 9 |
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 · 3
- _wp_scan_utf8()Finds spans of valid and invalid UTF-8 bytes in a given string.
- mb_ord()Compat function to mimic mb_ord().
- bin2hex()
Used by · 1
- render_block_core_social_link()Renders the `core/social-link` block on server.
Source code
function antispambot( $email_address, $hex_encoding = 0 ) { $obfuscated = ''; $at = 0; $end = strlen( $email_address ); $invalid_length = 0; while ( $at < $end ) { $was_at = $at; if ( 0 === _wp_scan_utf8( $email_address, $at, $invalid_length, null, 1 ) && 0 === $invalid_length ) { break; } $character_length = $at - $was_at; if ( $character_length > 0 ) { $character = substr( $email_address, $was_at, $character_length ); switch ( rand( 0, 1 + $hex_encoding ) ) { case 0: $code_point = mb_ord( $character ); $obfuscated .= "&#{$code_point};"; break; case 1: $obfuscated .= $character; break; case 2: for ( $i = 0; $i < $character_length; $i++ ) { $hex_value = bin2hex( $character[ $i ] ); $obfuscated .= "%{$hex_value}"; } break; } } if ( 0 !== $invalid_length ) { $obfuscated .= substr( $email_address, $at, $invalid_length ); } $at += $invalid_length; } return str_replace( '@', '@', $obfuscated );}Changelog
Introduced in 0.71. 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.