Heim  >  Artikel  >  Backend-Entwicklung  >  javascript中的escape和unescape函数的php实现

javascript中的escape和unescape函数的php实现

WBOY
WBOYOriginal
2016-07-25 08:46:141003Durchsuche
escape函数
  1. /**
  2. * js escape php 实现
  3. * @param $string the sting want to be escaped
  4. * @param $in_encoding
  5. * @param $out_encoding
  6. */
  7. function escape($string, $in_encoding = 'UTF-8',$out_encoding = 'UCS-2') {
  8. $return = '';
  9. if (function_exists('mb_get_info')) {
  10. for($x = 0; $x $str = mb_substr ( $string, $x, 1, $in_encoding );
  11. if (strlen ( $str ) > 1) { // 多字节字符
  12. $return .= '%u' . strtoupper ( bin2hex ( mb_convert_encoding ( $str, $out_encoding, $in_encoding ) ) );
  13. } else {
  14. $return .= '%' . strtoupper ( bin2hex ( $str ) );
  15. }
  16. }
  17. }
  18. return $return;
  19. }
复制代码

unescape代码:

  1. function unescape($str)
  2. {
  3. $ret = '';
  4. $len = strlen($str);
  5. for ($i = 0; $i {
  6. if ($str[$i] == '%' && $str[$i + 1] == 'u')
  7. {
  8. $val = hexdec(substr($str, $i + 2, 4));
  9. if ($val $ret .= chr($val);
  10. else
  11. if ($val $ret .= chr(0xc0 | ($val >> 6)) .
  12. chr(0x80 | ($val & 0x3f));
  13. else
  14. $ret .= chr(0xe0 | ($val >> 12)) .
  15. chr(0x80 | (($val >> 6) & 0x3f)) .
  16. chr(0x80 | ($val & 0x3f));
  17. $i += 5;
  18. } else
  19. if ($str[$i] == '%')
  20. {
  21. $ret .= urldecode(substr($str, $i, 3));
  22. $i += 2;
  23. } else
  24. $ret .= $str[$i];
  25. }
  26. return $ret;
  27. }
复制代码


escape, javascript, php


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn