Jump to content

Can anyone convert this javascript to php


pepsi599ml

Recommended Posts

Hi, I'm wondering if it's possible to convert this javascript code to php.

The code itself converts Chinese (gb2312 encoding) characters to unicode format (&#x####;).

I know iconv can do this, but some special characters are lost during the conversion anyway.

And I found this simple javascript does the work very well, so here it is, just a line.

 

str.value = str.value.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"&#x$2;")});

think you're looking for rawurlencode.

 

js example (from w3schools.com):

 

<script type="text/javascript">
document.write(escape("Visit W3Schools!") + "<br />");
document.write(escape("?!=()#%&"));
</script>

 

vs.

 

php

 

echo rawurlencode("Visit W3Schools!") ."<br/>";
echo rawurlencode("? !=()#%&");

 

js output:

Visit%20W3Schools%21

%3F%21%3D%28%29%23%25%26

 

php output:

Visit%20W3Schools%21

%3F%21%3D%28%29%23%25%26

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.