Title: IdnaEncoder::encode
Published: March 29, 2023
Last modified: November 8, 2023

---

# IdnaEncoder::encode( string|WpOrgRequestsStringable $hostname ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wporg-requests-idnaencoder/encode/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wporg-requests-idnaencoder/encode/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wporg-requests-idnaencoder/encode/?output_format=md#source)

[ Back to top](https://developer.wordpress.org/reference/classes/wporg-requests-idnaencoder/encode/?output_format=md#wp--skip-link--target)

Encode a hostname using Punycode

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wporg-requests-idnaencoder/encode/?output_format=md#parameters)󠁿

 `$hostname`string|WpOrgRequestsStringablerequired

Hostname

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wporg-requests-idnaencoder/encode/?output_format=md#return)󠁿

 string Punycode-encoded hostname

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wporg-requests-idnaencoder/encode/?output_format=md#source)󠁿

    ```php
    public static function encode($hostname) {
    	if (InputValidator::is_string_or_stringable($hostname) === false) {
    		throw InvalidArgument::create(1, '$hostname', 'string|Stringable', gettype($hostname));
    	}

    	$parts = explode('.', $hostname);
    	foreach ($parts as &$part) {
    		$part = self::to_ascii($part);
    	}

    	return implode('.', $parts);
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/requests/src/idnaencoder.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/Requests/src/IdnaEncoder.php#L61)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/Requests/src/IdnaEncoder.php#L61-L72)

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwporg-requests-idnaencoder%2Fencode%2F)
before being able to contribute a note or feedback.