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;")});

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.