-2

I retrieve some string like (A00001) from mysql. I want to separate these like A and 00001.

After separation that values increased one (A00002), these process will do, when click submit button.

  • Are your data always in this format : 6 characters (1 Letter + 5 digits) ? – fdehanne Jun 24 '14 at 07:33
  • 1
    For the record, you need not tamper with it, PHP can handle incrementing strings like that. `$a = "A00001"; var_dump(++$a)` will result in "A00002". – Maerlyn Jun 24 '14 at 07:49
  • please suggest for this question....get value from table row and set into text box. see my jsfiddle file [ http://jsfiddle.net/yXFZs/ ] – user3758953 Jun 25 '14 at 12:56

5 Answers5

2

Something like this may do the work

$string = "A00001";

preg_match_all('/[^\d]+/', $string, $textArray);
preg_match('/\d+/', $string, $numbersArray);

$text = $textArray[0];
$number = $numbersArray[0];

originaly from Split String into Text and Number

Community
  • 1
  • 1
Athafoud
  • 2,898
  • 3
  • 40
  • 58
1
var text = "A00001";
var letters = text.replace(/[^a-z]/gi, ""); 
var digits = text.replace(/\D/g, "");
alert(letters);
alert(digits);

check this fiddle

Govind Singh
  • 15,282
  • 14
  • 72
  • 106
1
$value = 'A00001';
$letter = substr($value, 0, 1);
$number = (int) substr($value, 1)

$number++;

$value = $letter.str_pad($number, 5, '0', STR_PAD_LEFT);

str_pad() is used to add extra 0 in beginning of number if it as less than 5 characters.

fdehanne
  • 1,658
  • 1
  • 16
  • 32
0

If you know that the format is alqays the same (1 letter and 5 numbers), you can use that.

$string = 'A00001';
$letter = substr($string, 0, 1);
$number = substr($string, 1);

If there are any letters and any numbers, use the regex.

It´s for PHP, for JS it´ll be similar.

pavel
  • 26,538
  • 10
  • 45
  • 61
  • please suggest for this question....get value from table row and set into text box. see my jsfiddle file [ http://jsfiddle.net/yXFZs/ ] – user3758953 Jun 25 '14 at 12:55
0

In what language? If you want to extract in PHP you can use a regular expression. This example breaks out the letter and the digits and gives you an associative array:

preg_match('/^(?<letter>\w)(?<digits>\d+)$/', "A00001", $matches);
var_dump($matches);

Output:

Array
(
    [0] => A00001
    [letter] => A
    [1] => A
    [digits] => 00001
    [2] => 00001
)

In JavaScript you can adapt the same technique:

var str = "A00001";
var matches = str.match(/(\w)(\d+)/);
console.log(matches);  // ["A00001", "A", "00001"]
Eric
  • 18,532
  • 2
  • 34
  • 39
  • please suggest for this question....get value from table row and set into text box. see my jsfiddle file [ http://jsfiddle.net/yXFZs/ ] – user3758953 Jun 25 '14 at 12:54