- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在客户端将一些 PHP 转换为 javascript。
有人能给我指出这句话的正确翻译吗?如果需要外部库也没有问题。
$a = pack("A32", "foobar");
$b = implode("", unpack("H32", ("foobar", "frigobar")));
最佳答案
您可以在 JavaScript 中找到 PHP pack
函数的实现,网址为 locutus.io (它相当长,所以我没有将其包含在这里)。
PHP 的 implode
函数等同于 JavaScript 的 join
:
implode(",", array("first", "second", "third")) // "first,second,third"
与
相同["first", "second", "third"].join(",") // "first,second,third"
<小时/>
Locutus 还创建了一个 unpack
实现,但已从他的网站中删除。就是这样(尽管它被标记为“未准备好生产”)
function unpack(format, data) {
// http://kevin.vanzonneveld.net
// + original by: Tim de Koning (http://www.kingsquare.nl)
// + parts by: Jonas Raoni Soares Silva - http://www.jsfromhell.com
// + parts by: Joshua Bell - http://cautionsingularityahead.blogspot.nl/
// +
// + bugfixed by: marcuswestin
// % note 1: Float decoding by: Jonas Raoni Soares Silva
// % note 2: Home: http://www.kingsquare.nl/blog/22-12-2009/13650536
// % note 3: Feedback: phpjs-unpack@kingsquare.nl
// % note 4: 'machine dependant byte order and size' aren't
// % note 5: applicable for JavaScript unpack works as on a 32bit,
// % note 6: little endian machine
// * example 1: unpack('d', "\u0000\u0000\u0000\u0000\u00008YÀ");
// * returns 1: { "": -100.875 }
var formatPointer = 0, dataPointer = 0, result = {}, instruction = '',
quantifier = '', label = '', currentData = '', i = 0, j = 0,
word = '', fbits = 0, ebits = 0, dataByteLength = 0;
// Used by float decoding - by Joshua Bell
//http://cautionsingularityahead.blogspot.nl/2010/04/javascript-and-ieee754-redux.html
var fromIEEE754 = function(bytes, ebits, fbits) {
// Bytes to bits
var bits = [];
for (var i = bytes.length; i; i -= 1) {
var byte = bytes[i - 1];
for (var j = 8; j; j -= 1) {
bits.push(byte % 2 ? 1 : 0); byte = byte >> 1;
}
}
bits.reverse();
var str = bits.join('');
// Unpack sign, exponent, fraction
var bias = (1 << (ebits - 1)) - 1;
var s = parseInt(str.substring(0, 1), 2) ? -1 : 1;
var e = parseInt(str.substring(1, 1 + ebits), 2);
var f = parseInt(str.substring(1 + ebits), 2);
// Produce number
if (e === (1 << ebits) - 1) {
return f !== 0 ? NaN : s * Infinity;
}
else if (e > 0) {
return s * Math.pow(2, e - bias) * (1 + f / Math.pow(2, fbits));
}
else if (f !== 0) {
return s * Math.pow(2, -(bias-1)) * (f / Math.pow(2, fbits));
}
else {
return s * 0;
}
}
while (formatPointer < format.length) {
instruction = format.charAt(formatPointer);
// Start reading 'quantifier'
quantifier = '';
formatPointer++;
while ((formatPointer < format.length) &&
(format.charAt(formatPointer).match(/[\d\*]/) !== null)) {
quantifier += format.charAt(formatPointer);
formatPointer++;
}
if (quantifier === '') {
quantifier = '1';
}
// Start reading label
label = '';
while ((formatPointer < format.length) &&
(format.charAt(formatPointer) !== '/')) {
label += format.charAt(formatPointer);
formatPointer++;
}
if (format.charAt(formatPointer) === '/') {
formatPointer++;
}
// Process given instruction
switch (instruction) {
case 'a': // NUL-padded string
case 'A': // SPACE-padded string
if (quantifier === '*') {
quantifier = data.length - dataPointer;
} else {
quantifier = parseInt(quantifier, 10);
}
currentData = data.substr(dataPointer, quantifier);
dataPointer += quantifier;
if (instruction === 'a') {
currentResult = currentData.replace(/\0+$/, '');
} else {
currentResult = currentData.replace(/ +$/, '');
}
result[label] = currentResult;
break;
case 'h': // Hex string, low nibble first
case 'H': // Hex string, high nibble first
if (quantifier === '*') {
quantifier = data.length - dataPointer;
} else {
quantifier = parseInt(quantifier, 10);
}
currentData = data.substr(dataPointer, quantifier);
dataPointer += quantifier;
if (quantifier > currentData.length) {
throw new Error('Warning: unpack(): Type ' + instruction +
': not enough input, need ' + quantifier);
}
currentResult = '';
for (i = 0; i < currentData.length; i++) {
word = currentData.charCodeAt(i).toString(16);
if (instruction === 'h') {
word = word[1] + word[0];
}
currentResult += word;
}
result[label] = currentResult;
break;
case 'c': // signed char
case 'C': // unsigned c
if (quantifier === '*') {
quantifier = data.length - dataPointer;
} else {
quantifier = parseInt(quantifier, 10);
}
currentData = data.substr(dataPointer, quantifier);
dataPointer += quantifier;
for (i = 0; i < currentData.length; i++) {
currentResult = currentData.charCodeAt(i);
if ((instruction === 'c') && (currentResult >= 128)) {
currentResult -= 256;
}
result[label + (quantifier > 1 ?
(i + 1) :
'')] = currentResult;
}
break;
case 'S': // unsigned short (always 16 bit, machine byte order)
case 's': // signed short (always 16 bit, machine byte order)
case 'v': // unsigned short (always 16 bit, little endian byte order)
if (quantifier === '*') {
quantifier = (data.length - dataPointer) / 2;
} else {
quantifier = parseInt(quantifier, 10);
}
currentData = data.substr(dataPointer, quantifier * 2);
dataPointer += quantifier * 2;
for (i = 0; i < currentData.length; i += 2) {
// sum per word;
currentResult = ((currentData.charCodeAt(i + 1) & 0xFF) << 8) +
(currentData.charCodeAt(i) & 0xFF);
if ((instruction === 's') && (currentResult >= 32768)) {
currentResult -= 65536;
}
result[label + (quantifier > 1 ?
((i / 2) + 1) :
'')] = currentResult;
}
break;
case 'n': // unsigned short (always 16 bit, big endian byte order)
if (quantifier === '*') {
quantifier = (data.length - dataPointer) / 2;
} else {
quantifier = parseInt(quantifier, 10);
}
currentData = data.substr(dataPointer, quantifier * 2);
dataPointer += quantifier * 2;
for (i = 0; i < currentData.length; i += 2) {
// sum per word;
currentResult = ((currentData.charCodeAt(i) & 0xFF) << 8) +
(currentData.charCodeAt(i + 1) & 0xFF);
result[label + (quantifier > 1 ?
((i / 2) + 1) :
'')] = currentResult;
}
break;
case 'i': // signed integer (machine dependent size and byte order)
case 'I': // unsigned integer (machine dependent size & byte order)
case 'l': // signed long (always 32 bit, machine byte order)
case 'L': // unsigned long (always 32 bit, machine byte order)
case 'V': // unsigned long (always 32 bit, little endian byte order)
if (quantifier === '*') {
quantifier = (data.length - dataPointer) / 4;
} else {
quantifier = parseInt(quantifier, 10);
}
currentData = data.substr(dataPointer, quantifier * 4);
dataPointer += quantifier * 4;
for (i = 0; i < currentData.length; i += 4) {
currentResult =
((currentData.charCodeAt(i + 3) & 0xFF) << 24) +
((currentData.charCodeAt(i + 2) & 0xFF) << 16) +
((currentData.charCodeAt(i + 1) & 0xFF) << 8) +
((currentData.charCodeAt(i) & 0xFF));
result[label + (quantifier > 1 ?
((i / 4) + 1) :
'')] = currentResult;
}
break;
case 'N': // unsigned long (always 32 bit, little endian byte order)
if (quantifier === '*') {
quantifier = (data.length - dataPointer) / 4;
} else {
quantifier = parseInt(quantifier, 10);
}
currentData = data.substr(dataPointer, quantifier * 4);
dataPointer += quantifier * 4;
for (i = 0; i < currentData.length; i += 4) {
currentResult =
((currentData.charCodeAt(i) & 0xFF) << 24) +
((currentData.charCodeAt(i + 1) & 0xFF) << 16) +
((currentData.charCodeAt(i + 2) & 0xFF) << 8) +
((currentData.charCodeAt(i + 3) & 0xFF));
result[label + (quantifier > 1 ?
((i / 4) + 1) :
'')] = currentResult;
}
break;
case 'f': //float
case 'd': //double
ebits = 8;
fbits = (instruction === 'f') ? 23 : 52;
dataByteLength = 4;
if (instruction === 'd') {
ebits = 11;
dataByteLength = 8;
}
if (quantifier === '*') {
quantifier = (data.length - dataPointer) / dataByteLength;
} else {
quantifier = parseInt(quantifier, 10);
}
currentData = data.substr(dataPointer, quantifier * dataByteLength);
dataPointer += quantifier * dataByteLength;
for (i = 0; i < currentData.length; i += dataByteLength) {
data = currentData.substr(i, dataByteLength);
bytes = [];
for (j = data.length - 1; j >= 0; --j) {
bytes.push(data.charCodeAt(j));
}
result[label + (quantifier > 1 ?
((i / 4) + 1) :
'')] = fromIEEE754(bytes, ebits, fbits);
}
break;
case 'x': // NUL byte
case 'X': // Back up one byte
case '@': // NUL byte
if (quantifier === '*') {
quantifier = data.length - dataPointer;
} else {
quantifier = parseInt(quantifier, 10);
}
if (quantifier > 0) {
if (instruction === 'X') {
dataPointer -= quantifier;
} else {
if (instruction === 'x') {
dataPointer += quantifier;
} else {
dataPointer = quantifier;
}
}
}
break;
default:
throw new Error('Warning: unpack() Type ' + instruction +
': unknown format code');
}
}
return result;
}
<小时/>
因此,最后,您的示例将如下所示:
var a = pack("A32", "foobar");
var b = unpack("H32", ["foobar", "frigobar"]).join("");
关于javascript - 将 PHP 代码转换为 Javascript(内爆、打包和解包),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40048629/
我有一个多选字段 1 Months 2 Months 3 Months 我的 PHP 代码内爆多个值,即 123 作为 1,2,3 并将其插入数据库。问题是该字段不是必填字段,当我将其留空时会出现
片段: $temp = array(); foreach ($data as $key => $value) { // This line varies, from HTML to URLs,
我有一个带有查询的脚本,该查询使用连接从两个单独的表中的数据库收集信息。现在我想将这 2 个数组及其值并排插入到一个新表中。 $query = "INSERT INTO `new_table`
我需要从一个表中选择所有电子邮件地址,但通过 ; 对它们进行内爆。我是否可以仅使用单个 MySQL 查询来执行此操作? 最佳答案 是的, 与 GROUP_CONCAT .但您应该知道,默认的最大返回长
我还是 PHP 的初学者。所以我有一个名为“全名”的变量,我试图 explode 和内爆第一个和最后一个变量值。 $fullname='Andre Filipe da Costa Ferreira';
这个问题在这里已经有了答案: Implode a column of values from a two dimensional array [duplicate] (3 个答案) 关闭 4 个月前
内爆一个简单的数组 看起来像这样 $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $arra
我有一个 php 表单,它有一个复选框选项,如果用户选择“其他”,则会出现一个文本输入。这运行良好,但未提交数据。我收到错误消息 PHP implode():传递的参数无效 这是: PHP验证 if(
我的 js 文件中有一个数组。在用户选择他喜欢的座位后,数组中的数据创建就完成了。 然后我将它发送到一个php文件中进行显示。javascript代码如下所示。 $('#btnsubmit').cli
我有一个数组 $array = array("连衣裙","套装"); 当我尝试使用 phpredis 制作 BITOP 时 $value = implode(",",$array); $redis->
我选择了 (2) 个元素,通过以下方式选择: $(this).parents("tr:first").children("td").children("span"); 当我这样做时: $(this).
以下脚本生成包含所需数据的 txt 文件,但它在文本文件中生成了两次数据。 mysql_connect($hostname_MySQLCon, $username_MySQLCon, "") or
以下脚本生成包含所需数据的 txt 文件,但它在文本文件中生成了两次数据。 mysql_connect($hostname_MySQLCon, $username_MySQLCon, "") or
假设我有两个字符串要用定界符“连接”。 String s1 = "aaa", s2 = "bbb"; // input stringsString s3 = s1 + "-" + s2; // joi
implode() 函数适用于普通数组,但不适用于使用 mysql_fetch_array 创建的数组(我也试过 mysql_fetch_row) 如何让它们工作? 定义如上: $friends =
我有以下查询尝试在 CodeIgniter 中更新: $sql = "UPDATE fanout.manual_data SET call_leader_id = ?
现在已经为此苦苦挣扎了一段时间,可能很简单...... 我在尝试提交联系表单时不断收到以下错误 Warning: implode() [function.implode]: Invalid argum
我收到以下错误... 警告:implode() [function.implode]:在第 1335 行的\wp-content/themes/mytheme/functions.php 中传递的参数
我需要在客户端将一些 PHP 转换为 javascript。 有人能给我指出这句话的正确翻译吗?如果需要外部库也没有问题。 $a = pack("A32", "foobar"); $b = implo
我有以下数组 $appArray=array('a', 'b', 'c'); 我想生成类似'a\nb\nc\n' 的输出。问题是当我使用 implode('\n', $appArray) 我得到 'a
我是一名优秀的程序员,十分优秀!