Jump to content

Need help...


ignas2526

Recommended Posts

Hello,I trying to create function what will code text in javascript. Reason i need what not because i want to hide something but just simply make it not sow if javascript not turned on. What function must make text in something like "%70%61%67%65%20%6F%6E%65".Here is code:

echo escapeTxt("testing");function escapeTxt($os){$ns='';$t;$chr='';$cc='';$tn='';for($i=0;$i<256;$i++){$tn=$i.toString(16);if($tn.length<2)$tn="0"+$tn;$cc+=$tn;$chr+=unescape('%'+tn);}$cc=$cc.toUpperCase();$os.replace(String.fromCharCode(13)+'',"%13");for($q=0;$q<$os.length;$q++){$t=$os.substr($q,1);for($i=0;$i<$chr.length;$i++){if($t==$chr.substr($i,1)){$t=$t.replace($chr.substr($i,1),"%"+$cc.substr($i*2,2));$i=$chr.length;}}$ns+=$t;}return $ns;}

It drops me: error Fatal error: Call to undefined function tostring() in /public_html/test.php on line 9Thanks.

Link to comment
Share on other sites

You've got PHP and JavaScript seriously mixed up there. Read the PHP manual and find out the PHP equivalents for your JS code.Just putting $ signs before your variables won't magically turn JS code into PHP code. Sorry :)Oh yes - you also need to declare escapeTxt() before you call it.

Link to comment
Share on other sites

Ah, nice knowledge, gradz. I will better go and read "manuals" :) .BTW any good page wt php functions explaining and examples? like W3C----Edit:well i can just create something like: if A then 00 if B then 01 and % using arrays... but its soooooo borrrring lol.

Link to comment
Share on other sites

The best place for PHP functions is the official manual: http://static.php.net/manual/en/index.phpThe W3C doesn't endorse or have anything to do with PHP.Some functions to help you on your way:ord()strlen()str_replace()

Link to comment
Share on other sites

Thanks for reply, now i trying to create it using arrays and str_replace, but i got some problems :) Here is code:

function javascriptencode($text) {$letter = array('0','1','2','3','4','5','6','7','8','9');$jscode = array('%30','%31','%32','%33','%34','%35','%36','%37','%38','%39');$output = str_replace($letter, $jscode, $text);return $output;}echo javascriptencode("239082473");

Outpust must be:%32%33%39%30%38%32%34%37%33But i getting:%%332%33%39%%330%38%%332%34%37%33Any ideas what i did wrong :) ?Thanks.

Link to comment
Share on other sites

I didn't realize str_replace worked like that, it looks like all it's doing is basically calling str_replace for each find/replace pair and is using the previous replacement as the string to replace on. That means that when it gets to 3 to replace it with %33, it's finding the 3s in %30, %31, etc and replacing them. Try this version instead:

function javascriptencode($text) {  $output = '';  for ($i = 0; $i < strlen($text); $i++)	$output .= '%' . dechex(ord($text[$i]));  return $output;}

Link to comment
Share on other sites

Thanks for help, and time!Problem is what i don't understand 75% of PHP functions, for example i cant get what outputs ord... But i will try :) .Thanks again for help.

I didn't realize str_replace worked like that, it looks like all it's doing is basically calling str_replace for each find/replace pair and is using the previous replacement as the string to replace on. That means that when it gets to 3 to replace it with %33, it's finding the 3s in %30, %31, etc and replacing them. Try this version instead:
function javascriptencode($text) {  $output = '';  for ($i = 0; $i < strlen($text); $i++)	$output .= '%' . dechex(ord($text[$i]));  return $output;}

Link to comment
Share on other sites

That's right. This line:$output .= '%' . dechex(ord($text[$i]));Takes the ASCII (decimal) value for the character using ord, converts it to hex with the dechex function, and adds the % symbol to the front of it. This would have a problem if the hex value was less than 0x10, because it would only put one hex character (it wouldn't pad with a leading zero), but other than that it will work fine.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...