Posted By

hanguofeng on 04/09/09


Tagged

escape js String encode


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


escape in php


 / Published in: PHP
 

URL: http://topic.csdn.net/u/20071006/18/34e13f63-970a-4bb8-8671-20d7fd701fe5.html?632863885

  1. function escape($str)
  2. {
  3. preg_match_all("/[\xc2-\xdf][\x80-\xbf]+|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}|[\x01-\x7f]+/e",$str,$r);
  4. //匹配utf-8字符,
  5. $str = $r[0];
  6. $l = count($str);
  7. for($i=0; $i <$l; $i++)
  8. {
  9. $value = ord($str[$i][0]);
  10. if($value < 223)
  11. {
  12. $str[$i] = rawurlencode(utf8_decode($str[$i]));
  13. //先将utf8编码转换为ISO-8859-1编码的单字节字符,urlencode单字节字符.
  14. //utf8_decode()的作用相当于iconv("UTF-8","CP1252",$v)。
  15. }
  16. else
  17. {
  18. $str[$i] = "%u".strtoupper(bin2hex(iconv("UTF-8","UCS-2",$str[$i])));
  19. }
  20. }
  21. return join("",$str);
  22. }

Report this snippet  

You need to login to post a comment.