urlencode_deep( mixed $value ): mixed

Navigates through an array, object, or scalar, and encodes the values to be used in a URL.

Parameters

$valuemixedrequired
The array or string to be encoded.

Return

mixed The encoded value.

Source

function urlencode_deep( $value ) {	return map_deep( $value, 'urlencode' ); } 

Changelog

VersionDescription
2.2.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Here is an example for string:

    echo urlencode_deep( 'foo bar' ); // Output will be: foo+bar

    Here is an example for array:

    $data = urlencode_deep( array( 'foo' => 'bar long text', 'key1' => 'value1' ) ); // Output will be for $data: array( 'foo' => 'bar+long+text', 'key1' => 'value1' )

    Here is a use case for multi word string as url parameter:

    echo add_query_arg( 'key', urlencode_deep( 'long value' ), 'http://example.com/' ); // Output will be: http://example.com/?key=long+value

    Here is a use case for an array multi word string as url parameter:

    $params = array( 'key1' => 'value1 long text', 'key2' => 'value2', ); echo add_query_arg( urlencode_deep( $params ), 'http://example.com/?' ); // Output will be: http://example.com/?key1=value1+long+text&key2=value2

    It’s making spaces between word, if string has space:

    echo add_query_arg( 'key', 'long value', 'http://example.com/' ); // Output will be: http://example.com/?key=long value

You must log in before being able to contribute a note or feedback.