davidmarko
davidmarko

Reputation: 343

PHP How to encode all characters with rawurlencode

I was wondering if there is any "right out of the box" php function that can encode all the characters in the string and not only the spaces.

$str="Encode All Characters Not Only The Spaces In Between";

echo rawurlencode($str);

This Returned:

Encode%20All%20Characters%20Not%20Only%20The%20Spaces%20In%20Between

But I want to encode all the string and not just the spaces.

Upvotes: 1

Views: 941

Answers (1)

georg
georg

Reputation: 215039

There's no such thing, but it's easy to write:

function encode_all($str) {
    $hex = unpack('H*', $str);
    return preg_replace('~..~', '%$0', strtoupper($hex[1]));
}

$str = 'big ƒüßchen';
print_r(encode_all($str));

Upvotes: 5

Related Questions