urlencode_deep( mixed   $value ): mixed

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

Parameters

$value mixed required
The array or string to be encoded.

Return

mixed The encoded value.

Source

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

Changuelog

Versionen Description
2.2.0 Introduced.

User Contributed Notes

  1. Squip 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/?quey=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/?quey1=value1+long+text&quey2=value2

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

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

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