手掌心
2021-06-21 15:10:42
我正在嘗試使用類似于 Rot13 的算法在 PHP 中對字符串進行編碼,然后在 JavaScript 中對字符串進行解碼并進行搜索和替換。它適用于 ASCII 字符,但不適用于 Unicode。我弄亂了附加的代碼,但無法讓它工作。<?phpfunction 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><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>一旦 JS 運行,它應該用解碼后的數據 [索引][1] 替換數據 [索引][0]。
2 回答

Helenr
TA貢獻1780條經驗 獲得超4個贊
我發現的一種解決方案:
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 字符。唯一的問題是,由于轉義,當涉及到字符串的大小時,它會增加一些開銷。

藍山帝景
TA貢獻1843條經驗 獲得超7個贊
(我沒有足夠的聲譽來發表評論,所以我求助于使用答案......)
不確定它是否有所不同,但在 HTML“h2”標題中,您的 Unicode 表達式是...
ü an ?. 棕色的狐貍跳了起來.
...在數據[]中,它是...
ü and ?. 棕色的狐貍跳了起來.
假設“an”和“and”應該是一樣的?
添加回答
舉報
0/150
提交
取消