1

I have a URL dynamically displayed with a PHP script. This URL comes to be the name of a CSS class. I need to use this class name into a jQuery script after an Ajax call response (All the HTML into this specific class has to be hidden).

The URL contains / and . and : — To make it easier in my jQuery script, I would like to convert the URL into an Integer with a PHP function (like hash("md5",)) ... and in my JavaScript, convert the URL again into an integer that will be obviously the same.

I read that How to calculate md5 hash of a file using javascript but it doesn't look like the best solution. Does anyone have a more intelligent solution?

Regards

Community
  • 1
  • 1
Vincent Roye
  • 2,751
  • 7
  • 33
  • 53
  • 4
    "This URL comes to be the name of a CSS class." ... What. – Ignacio Vazquez-Abrams Aug 01 '12 at 14:46
  • Why include it in the URL in the first place? Can you use `$_SESSION` to store this data? – Matt Aug 01 '12 at 14:46
  • 4
    Hashing the URL isn't a solution at all. A hash is, by definition, irreversible - hashing the URL would mean that you will never be able to regenerate it. You can always encode the URL using base64 if that's what you want. – Palladium Aug 01 '12 at 14:47
  • 1
    whaaat?? code!! We have no idea what you're even saying... – Mark Pieszak - Trilon.io Aug 01 '12 at 14:47
  • A more intelligent solution would be to not use an MD5 hash as a class name. MD5s are quite unfriendly semantically. – Robert K Aug 01 '12 at 14:48
  • Is this to allow you the ability to write css classes/rules unique to a particular url? As in, if 99.999% of the pages work with your rules, but for whatever reason, this page is special, chews your rules up and spits them out, and you don't want to risk breaking your other pages for this one stinking page? I could see some advantages to that. – JayC Aug 01 '12 at 14:56

1 Answers1

0

You are probably looking for encoding and not hashing, as you want to read the URL back. I'd try using base64 - on the server side: http://php.net/manual/en/function.base64-decode.php and http://php.net/manual/en/function.base64-encode.php

And on client (JavaScript) side: http://www.webtoolkit.info/javascript-base64.html, How can you encode a string to Base64 in JavaScript?

Community
  • 1
  • 1
jderda
  • 890
  • 6
  • 18
  • Not a bad idea, but he'll probably have to do some post-processing to make it work with css classes, due to the inclusion of characters "/" and "+". Furthermore, css class names are case insensitive within the ASCII range of characters, which leads to non-unique mappings. Still probably could fix that with more post-processing, though. – JayC Aug 01 '12 at 15:11
  • Finaly I just removed all the non alphanumeric characters with PHP and javascript and it's working fine :) – Vincent Roye Aug 01 '12 at 16:59