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.
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.
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
var text = "A00001";
var letters = text.replace(/[^a-z]/gi, "");
var digits = text.replace(/\D/g, "");
alert(letters);
alert(digits);
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.
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"]