tags:

views:

62

answers:

1

Hey guys, I'm not much of a PHP expert. I'm encoding a URL with base64_encode. I get quite a long encoded string with a lot of weird characters exactly as I want it to be. Is there a way to trim this long line of characters to let's say 10 or 15 chars, so I can decode it later again?

I know there is trim() but that does not exactly what I want. I want a long encoded string to be rather short and later I want to decode it again.

Any ideas?

+7  A: 

It's not possible to "shorten" any string without losing some data.

  • If you want to physically shorten an encoded string (with the end result being only part of that string), apply substr() but not on the encoded version: You need to decode it first, then re-encode the shortened version.

  • Another option is to compress a string. This may shorten it somewhat: Look into gzcompress(). Your mileage may vary, though: the compression rate will depend on what kind of data you have. With small input strings, the result can even be larger than the original.

  • If you want to reuse a variable in a multi-page process, and don't want to transport it through a link or a form, consider generating a short random key, and storing the data in the user's session:

    $_SESSION[$randomKey] = "lllloooooooooooong data here";
    

    You could pass on the random key, and always access the "long" data using $_SESSION[$randomKey]. You need to have a session initialized for this.

Pekka