javascript中的escape和unescape函數的php實現

來源:互聯網
上載者:User
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 < mb_strlen ( $string, $in_encoding ); $x ++) {
  11. $str = mb_substr ( $string, $x, 1, $in_encoding );
  12. if (strlen ( $str ) > 1) { // 多位元組字元
  13. $return .= '%u' . strtoupper ( bin2hex ( mb_convert_encoding ( $str, $out_encoding, $in_encoding ) ) );
  14. } else {
  15. $return .= '%' . strtoupper ( bin2hex ( $str ) );
  16. }
  17. }
  18. }
  19. return $return;
  20. }
複製代碼

unescape代碼:

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

escape, javascript, php
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.