Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have problem here we I would like to add on the integer part of a serial number. Say I read as JK001256. Thus I can use substring to remove the first 2 char and the rest 6 as numerics. The problem I want to add to the number say 50 means it starts from JK001256 to JK0012306. If I extra the numeric it get 1256 and lose the 00. Any ideas?
$stringVersionWithLeadingZeroes = sprintf('%06d', $numericPart)
http://php.net/manual/function.sprintf.php
Update
In order to provide something complete, try this
if (!preg_match('/^([A-Z]{2})(\d{6})$/', $serial, $parts)) {
throw new Exception('Invalid serial number');
}
$newSerial = sprintf('%s%06d', $parts[1], (int) $parts[2] + 50);
$serial = 'JK001256';
sscanf($serial, 'JK%d', $serial_no);
$new_serial = sprintf('JK%06d', $serial_no + 50);
var_dump($new_serial); // string(8) "JK001306"
Working demo: http://codepad.org/jbBnofUh
Any 2-chars prefix:
$serial = 'JK001256';
sscanf($serial, '%2s%6d', $serial_prefix, $serial_no);
$new_serial = sprintf('%s%06d', $serial_prefix, $serial_no + 50);
var_dump($new_serial); // string(8) "JK001306"
Demo: http://codepad.org/aE0Htgyh
Any non-digit prefix:
$serial = 'JK001256';
sscanf($serial, '%[^0123456789]%6d', $serial_prefix, $serial_no);
$new_serial = sprintf('%s%06d', $serial_prefix, $serial_no + 50);
var_dump($new_serial); // string(8) "JK001306"
Demo: http://codepad.org/aQksxJjU
Any non-digit prefix & unspecified digits length:
$serial = 'JK001256';
sscanf($serial, '%[^0123456789]%s', $serial_prefix, $serial_no);
$new_serial = sprintf('%s%0' . strlen($serial_no) . 'd', $serial_prefix, intval($serial_no) + 50);
var_dump($new_serial); // string(8) "JK001306"
Demo: http://codepad.org/fyYskTWP
$last_part = substr("JK001256", -2); // 56
$first_part = substr("JK001256",0, -2); // JK0012
$sum = $last_part + 50;
$new_string = $first_part.$sum; // JK0012306;
I dont know if its the best way to do it, but it should work
You can use str_pad:
<?php
$value1 = substr("JK001256",2, 6);
$first = substr("JK001256",0, 2);
$index = 50;
for($i=0;$i<=$index;$i++)
{
$value2 = $value1 + $i;
$new_index[] = $first."".str_pad($value2, 6, "0", STR_PAD_LEFT);
}
print_r($new_index);
?>
http://codepad.org/CAlJRkJ4
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How can I format a string to prepend UTC if it starts with + or - and add a : before the last two digits if it ends with 4 digits?
Examples and expected result:
PST > PST
+08 > UTC+08
-0845 > UTC-08:45
Thank you!
<?php
$string = "PST";
if (substr($string, 0, 1) === '+' || substr($string, 0, 1) === '-'){
if(strlen($string) == 3){
$newString = 'UTC'.$string;
}
else{
$newString = 'UTC'. substr($string, 0, 3). ':' .substr($string, -2, 2);
}
}
else{
$newString = $string;
}
echo $newString;
You don't need regex for that or at least regex seems overkill. You can simple create this logic using an if-else and modify your string accordingly
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to create an array of integers from strings of text composed of the integers separated by whitespace and plus signs, for example
$string = "1 + 2 + 3 + 4";
is extracted into
$array = ('1' , '2' ,'3' , '4');
This needs to be done in php.
Thank you in advance for any help.
Thankyou for the question refinment.
<?php
$string = '1 + 2 + 3';
$array = explode(' + ', $string);
for ($a=0;$a<count($array);$a++){
$array[$a] = (int) $array[$a]; // RECAST STRING TO INTEGER
}
?>
To get the array you need to use PHPs explode function, which will split a string into an array by a delimiter, for example:
$integerString = '1 + 2 + 3 + 4';
$integerArray = explode(' + ', $integerString);
Use preg_match_all!
<?php
$string = '1 + 2 + 3';
preg_match_all("'[0-9]+'sim", $string, $out);
print_r($out);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to split 4113.52318N in two parts like
41 and 13.52318N
And after that I want to remove N from the second value.
any help please?
There are several ways to do this, one is with preg_match_all, i.e.:
<?php
$string = "4113.52318N";
$result = preg_match_all('/^(\d{2})([\d.]+)/', $string, $matches);
$partOne = $matches[1][0]; //41
$partTwo = $matches[2][0]; //13.52318
Ideone Demo
Try this :
$part1 = substr('4113.52318N', 0, 2) // 41
$part2 = substr('4113.52318N', 3); // 13.52318N
$final = substr('4113.52318N', 0, -1); // 4113.52318
Use substr: http://php.net/manual/en/function.substr.php
$original = "4113.52318N";
$fortyone = substr($original, 0, 2); // 41
$other = substr($original, 3); // 13.52318N
$n_removed = substr($other, 0, -1); // 13.52318
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have generate a 16 digit number and i want to extract one number from every 4 digits of that 16 digit number. For e.g: 1234567892345678. I want to extract 2 from 1234, 7 from 5678, 3 from 9034 & 7 from 5678. Then store it in another variable $a. the extraction will be in a random manner.
You can try this -
$d = '1234567892345678';
$s = str_split($d, 4); // split in 4 digits
$n = array_map(function($x) {
return substr($x, rand(0, 3), rand(1, 1)); // extract single digit random number
}, $s);
$n will hold the random numbers.
I might be late at answering this question but you can simply use strlen function along with for loop like as
$str = "1234567892345678";
for($i = 0; $i < strlen($str);$i += 4){
echo $str[$i+rand(0,3)];
}
Here you have a one-liner:
$s = $string[rand(0,3)].$string[rand(4,7)].$string[rand(8,11)].$string[rand(12,15)];
echo $s;
Another one-liner:
for($s='',$i=0;$i<10; $s.=$string[rand($i,$i+=3)]);
echo $s;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I would like to set a whitespace between numbers bigger than 999.
So for example:
Not "1000" but "1 000",
Not "10500" but "10 500";
How to do it while formatting a string?
Try with number_format, something like this:
<?php
$number = 10500;
echo number_format($number, 0, '.', ' ');
// Output 10 500
?>
This should do the trick.
Try using number_format() function.
Example:
$a = 1000;
echo number_format($a, 0, '.', ' ');
You maybe looking for the php function number_format()
$number= 1000;
if($number > 999){
$number = number_format($number, 0, '', ' ');
echo $number;
}
DEMO
You could use the substr() function inside of php