Welcome Guest, Not a member yet? Register   Sign In
PHP Encode Jquery Decode
#1

[eluser]CoffeeBeanDesign[/eluser]
Hello Team CI

Just been working on some functions and I thought I would share. I'd be grateful if the experts among you would have a scan through the functions and tell me whether I can make any improvements. I've used email link encoding in the past and this is a development on the same theme.

General Encode Function

Code:
function jquery_encode($content = '', $noscript = '[content hidden by javascript]')
{
   $content = str_replace("'", "\'", $content);

   $ords = '';
    
   // convert to ASCII
   for ($i = 0; $i < strlen($content); $i++) :
    
      $ords .= "&#" . ord(substr($content, $i)) . ";";
        
   endfor;    
    
   // convert to hexadecimal
   $encoded = bin2hex("$ords");
   $encoded = chunk_split($encoded, 2, '%');
   $encoded = '%' . substr($encoded, 0, strlen($encoded) - 1);
  
   // return the output    
   return '<span class="encoded"><span class="_js_encoded_content">' . $encoded . '</span>' . $noscript . '</span>';
}


Jquery Decode Function

Code:
$(document).ready(function () {

// find each encoded span
$("span.encoded").each(function(){

   // get the encoded string
   encoded = $(this).find('._js_encoded_content').text();

   // unescape the string
   content = unescape(encoded);

   // create temp div with the content
   $(this).after('<div id="_temp_encoding_div">' + content + '</div>');

   // copy the content across
   $(this).html($("#_temp_encoding_div").text());

   // remove the temp div  
   $("#_temp_encoding_div").remove();

});

});


The temporary div is required so the browser can properly parse the html. I am using the function for email links but it could be useful elsewhere.

I look forward to your thoughts.
#2

[eluser]garymardell[/eluser]
Erm, i can't see what the point of your code is?
#3

[eluser]CoffeeBeanDesign[/eluser]
So....

Standard email encoding works by changing the content to a bunch of unintelligible characters which spammers can't read. The content is only visible if JavaScript is turned on but the source code looks like this :

Code:
<a 

The PHP encode, JS decode is an oft used technique to hide emails from spam robots. These two functions simplify the process and also open the scope to encode anything else - I though maybe telephone addresses etc.

I hope this makes sense.
#4

[eluser]garymardell[/eluser]
sorry, misunderstood the first paragraph. Thought you were saying it was similar to email encoding you've done in the past rather than email encoding with new developments. Never understood why not use an image. It would mean users with non javascript could see it and copy it down, you could also use javascript to take the image and replace it with text so they can copy and paste.
#5

[eluser]CoffeeBeanDesign[/eluser]
Fair point.

You could of course do this :

Code:
jquery_encode('[email protected]', img('email.png'));

Although I think I'm right in saying some clever spam bots can 'read' images too.

I'm no JS expert - if someone could write the equivalent JS functions for the other JS frameworks ( and maybe a bare JS version ) I could rename the function to a more general 'js_encode' and I'll add a wiki page.

Cheers.




Theme © iAndrew 2016 - Forum software by © MyBB