pepsi599ml Posted November 24, 2008 Share Posted November 24, 2008 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;")}); Quote Link to comment https://forums.phpfreaks.com/topic/133999-can-anyone-convert-this-javascript-to-php/ Share on other sites More sharing options...
pepsi599ml Posted November 24, 2008 Author Share Posted November 24, 2008 ok, looking at the code carefully and I realized what I'm looking for A php equivalent of javascript's escape function. tried urlencode but the result isn't what I expected. Quote Link to comment https://forums.phpfreaks.com/topic/133999-can-anyone-convert-this-javascript-to-php/#findComment-697509 Share on other sites More sharing options...
Mchl Posted November 24, 2008 Share Posted November 24, 2008 addslashes perhaps? or even addcslashes I believe will be a closer one [edit] Just reread how JS escape() works... those PHP functions won't help I'm afraid... htmlspecialchars maybe? Quote Link to comment https://forums.phpfreaks.com/topic/133999-can-anyone-convert-this-javascript-to-php/#findComment-697521 Share on other sites More sharing options...
.josh Posted November 27, 2008 Share Posted November 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/133999-can-anyone-convert-this-javascript-to-php/#findComment-700509 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.