如何将这个简短的PHP函数转换为Javascript? [英] How can I convert this short PHP function into Javascript?

查看:117
本文介绍了如何将这个简短的PHP函数转换为Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将此PHP代码复制到Javascript中?它需要一个像2-9999(序列号)的数字并将其转换为NUMBER ...转换时,2-999将是一个不同的值。

How can I replicate this PHP code into Javascript ?? It takes a number like 2-9999 (serial number) and converts it into a NUMBER ... 2-999 would be a different value when converted.

function esn_to_num($esn)
{

    if (($tmp = explode('-', $esn))) {

        if (sizeof($tmp) == 2
            && my_isnum($tmp[0])
            && my_isnum($tmp[1])
        ) {
            $esn = (($tmp[0] << 23) | $tmp[1]);
        } else {
            $esn = -1;
        }
    } else {
        $esn = -1;
    }

    return $esn;
}

我在下面添加了一个依赖函数

I added a dependency function below

// dependency function
    /*****************************************************************************
    * CHECKS IF A STRING REPRESENTS A NUMBER
    ******************************************************************************/
    function my_isnum($str, $negative=false, $decimal=false)
    {
        $has_decimal = false;
        $len = strlen($str);
        if ($len > 0) {
            $valid = true;
            for ($i=0; $valid && $i<$len; $i++) {
                if (!($str[$i] >= '0' && $str[$i] <= '9')) {
                    if ($str[$i] == '-') {
                        if (!$negative || $i != 0) {
                            $valid = false;
                        }
                    } else if ($str[$i] == '.') {
                        if (!$decimal || $has_decimal) {
                            $valid = false;
                        }
                    } else {
                        $valid = false;
                    }
                }
            }
        } else {
            $valid = false;
        }
        return $valid;
    }


推荐答案

php语法非常类似于JS。

php syntax is pretty similar to JS.

只需获取变量并删除 $ ,并使用声明它们var 关键字。

Just take the variables and remove the $, and declare them with the var keyword.

function esn_to_num(esn) {
    var tmp = [];
    if ((tmp = explode('-', $esn))) {
        if (sizeof(tmp) == 2
            && my_isnum(tmp[0])
            && my_isnum(tmp[1])
        ) {
            esn = ((tmp[0] << 23) | tmp[1]);
        } else {
            esn = -1;
        }
    } else {
        esn = -1;
    }
    return esn;
}

然后你需要用等效的JS替换所有的php函数。查找javascript等价爆炸 sizeof

Then you need to replace all your php functions with equivalent JS ones. Look up "javascript equivalents of explode, sizeof.

按照相同的过程在javascript中重写 my_isnum (显然不是原生的php函数)。

Follow the same process to rewrite my_isnum in javascript (clearly not a native php function).

编辑:在看到你的更新之后,我意识到Stack Overflow(或其他地方)可能已经存在很多代码,这些代码将是 my_isnum 所做的。 js函数确定字符串是否为数字或在此处单独询问。

edit: after seeing your update, I realize there is probably already a good chunk of code on Stack Overflow (or elsewhere) which will what my_isnum does. Either search "js function to determine if string is number" or ask that in a seperate question here.

您可以在chromes javascript控制台中完成所有这些操作,并继续迭代直到停止如果你遇到一个特定的问题并且卡住了,那么在Stack Overflow上提出这个问题是个好点。

You can do all this in chromes javascript console, and keep iterating until you stop getting errors. If you run into a specific problem and are stuck, thats a good point to ask about it on Stack Overflow.

这篇关于如何将这个简短的PHP函数转换为Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆