-2

Hi few times ago I post question how to add eight 0 before primary key using php and javascript and I got my answers for both php and javascript and I create two functions for it.

PHP function

function addingZerosBeforeIds($ids){
     return str_pad($ids, 9, "0", STR_PAD_LEFT);
}

and javascript function

function addingZerosBeforeIds(input, length, string) {
     string = string || '0'; input = input + '';
     return input.length >= length ? input : new Array(length - input.length + 1).join(string) + input;
}

its working fine but now I want to remove zeros before primary key I tries a lot for this but didn't succeed how to do this.

Community
  • 1
  • 1
user3833682
  • 263
  • 5
  • 20

3 Answers3

1

in js you can simply do it like

parseInt("000123", 10); //123

and in php

intval("000123"); //123
Asgu
  • 712
  • 5
  • 15
  • doing parseInt("000123") without passing the radix will sometimes cause it to parse it as an octet. You must always pass in the radix. so parseInt("000123", 10) will insure that it gets parsed as base10 number, see this question. http://stackoverflow.com/questions/850341/how-do-i-work-around-javascripts-parseint-octal-behavior – Jonathan Apr 27 '15 at 14:13
0
<?php
    function removeZerosBeforeIds($input) {
        return preg_replace('/^0*/','',$input);
    }
Med
  • 2,035
  • 20
  • 31
0

you could simply just parse the Int.

javascript

str = parseInt(str, 10);

php

str = (int)$str;
Jonathan
  • 2,778
  • 13
  • 23