Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9229157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:30:10+00:00 2026-06-18T05:30:10+00:00

How can I replicate this PHP code into JavaScript? It takes a number like

  • 0

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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:30:12+00:00Added an answer on June 18, 2026 at 5:30 am

    php syntax is pretty similar to JS.

    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;
    }
    

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

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

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now I have my website built on PHP & Mysql. Consider this like a
I am trying to replicate this using PHP: 2011-03-30;0;0;0;0\n2011-03-30;0;0;0;0\n2011-03-30;0;0;0;0\n2011-03-30;0;0;0;0\n2011-03-30;0;0;0;0\n2011-03-30;0;0;0;0\n2011-03-30;0;0;0;0 Although when I am doing
I know a view is composed of tables. Can I replicate a view anyway?
Wondering if anyone can suggest a good file replication tool that will replicate across
I'm trying to replicate some of the functionality in Apple's avTouch, but I can't
I need to replicate memcached to another key value system (couchbase). How can I
I am writing for Android, and I can't figure out how to replicate the
I'm currently using PHP to include multiple css (or js) files into a single
Is it possible to access session variables made in from JavaScript, and how can
MongoDB's excellent PHP driver allows me to write to write PHP code that looks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.