wppaste
WordPress

antispambot( string $email_address, int $hex_encoding = 0 ): string

Since
0.71, 7.1.0
Source
wp-includes/formatting.php:2952
Obscures email addresses in HTML to prevent spam bots from harvesting them.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
12–24

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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.

WhenInstructionsCalls it makes
!$at12none
$at && $invalid_length === 024_wp_scan_utf8()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7112–2491 more instruction than PHP 8.5
8.57012–249
8.47012–2499 fewer instructions than PHP 8.3
8.37915–279
8.27915–2791 more instruction than PHP 8.1
8.17815–2791 fewer instruction than PHP 7.4
7.47915–279

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

Used by · 1

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( '@', '&#64;', $obfuscated );}

Changelog

Introduced in 0.71. 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.

7.1.0
Masquerades multibyte characters.from the docblock
0.71
Introduced.from the docblock

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.