首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果包含Unicode字符,PHP中的编码字符串无法在JavaScript中解码。

如果包含Unicode字符,PHP中的编码字符串无法在JavaScript中解码。
EN

Stack Overflow用户
提问于 2019-06-09 23:41:50
回答 2查看 168关注 0票数 0

我正在尝试使用一种类似于Rot13的算法在PHP中编码一个字符串,然后在JavaScript中解码该字符串并进行搜索和替换。它可以很好地处理ASCII字符,但不能处理Unicode。

我已经摆弄了附加的代码,但不能让它工作。

代码语言:javascript
复制
<?php

function strRot($str, $n) {
    $len = mb_strlen($str);
    $min = 0;
    $max = 99999999;
    $final = '';

    for ($i = 0; $i < $len; $i++) {
        $current = mb_ord($str[$i]);
        $val = $current+$n;

        if ($val >= $max) {
            $val = $val - $max;
        }

        if ($val <= $min) {
            $val = $val + $min;
        }

        $final .= mb_chr($val);
    }

    return $final;
}

?><!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">

    <title>Hello, world!</title>
</head>
<body>
    <h1>Hello, world!</h1>
    <h2>Ü and ?. 棕色的狐狸跳了起来.</h2>

    <p>The Hello, world! expression will be replaced.</p>
    <p>Ü and ?. 棕色的狐狸跳了起来. Should be replaced too.</p>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>

    <script id="scriptId" type="text/javascript">
        var data = [
            ["Hello, world!", "<?php echo base64_encode(strRot('I got replaced.', 1000)); ?>"],
            ["Ü and ?. 棕色的狐狸跳了起来.", "<?php echo base64_encode(strRot('? before Ü and 棕色的.', 1000)); ?>"]
        ];

        function b64DecodeUnicode(str) {
            // Going backwards: from bytestream, to percent-encoding, to original string.
            return decodeURIComponent(atob(str).split('').map(function(c) {
                return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
            }).join(''));
        }

        function strRot(str, n)
        {
            var min = 0;
            var max = 99999999;
            var final = '';

            for (var i in str) {
                var current = str.charCodeAt(i);
                var val = current+n;

                if (val >= max) {
                    val = val - max;
                }

                if (val <= min) {
                    val = val + min;
                }

                final += String.fromCharCode(val);
            }

            return final;
        }

        function replace() {
            for (index in data) {
                //var regex = new RegExp(data[index][0], "ug");
                jQuery("html *:not(script[id=scriptId])").children().each(function () {
                    jQuery(this).html(jQuery(this).html().replace(
                        data[index][0],
                        strRot(b64DecodeUnicode(data[index][1]), -1000)
                    ));
                });
            }
        }

        replace();
    </script>

</body>
</html>

一旦JS运行,它将用解码的dataindex替换dataindex。

EN

回答 2

Stack Overflow用户

发布于 2019-06-10 01:55:45

(我没有足够的名气来评论,所以我求助于使用答案...)

我不确定这是否有什么不同,但是在HTML "h2“头中,您的Unicode表达式是...

代码语言:javascript
复制
Ü an ?. 棕色的狐狸跳了起来.

data[]中的...and,它是...

代码语言:javascript
复制
Ü and ?. 棕色的狐狸跳了起来.

假设"an“和" and”应该是一样的?

票数 0
EN

Stack Overflow用户

发布于 2019-06-11 10:05:33

我找到了一个解决方案:

代码语言:javascript
复制
var data = [
            ["Hello, world!", "<?php echo base64_encode(strRot(rawurlencode('I got replaced.'), 1000)); ?>"],
            ["Ü and ?. 棕色的狐狸跳了起来.", "<?php echo base64_encode(strRot(rawurlencode('? before Ü and 棕色的.'), 1000)); ?>"]
        ];

// Then, in replace():

decodeURIComponent(strRot(b64DecodeUnicode(data[index][1]), -1000))

这之所以有效,是因为它在旋转字符之前对所有unicode字符进行转义。唯一的问题是,当涉及到字符串的大小时,由于转义,它增加了一些开销。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56515990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档