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 5 years ago.
Improve this question
I need to generate unique ticket number for every booking add should generate one unique id
Ex:B0000000001
Try with random_string in Codeigniter
Syntax
random_string([$type = 'alnum'[, $len = 8]])
Available Types($type) are
alpha - A string with lower and uppercase letters only.
alnum - Alpha-numeric string with lower and uppercase characters.
basic - A random number based on mt_rand().
numeric - Numeric string.
nozero - Numeric string with no zeros.
md5 - An encrypted random number based on md5() (fixed length of 32).
sha1 - An encrypted random number based on sha1() (fixed length of
40).
Read More about random_string
Try this
$uniqueNumber = strftime("%Y%m%d%H%M%S");
for generating unique number each time based on time.
You can try using str_pad
Code
$max = 9;
for($x = 1; $x <= 11; $x++){
echo 'B' .str_pad('', $max - strlen((string) $x), '0', STR_PAD_LEFT) . $x . "<br />";
}
Result
B000000001
B000000002
B000000003
B000000004
B000000005
B000000006
B000000007
B000000008
B000000009
B000000010
B000000011
Related
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
For example if I had:
1.2
1.65
5
9.5
125
Valid numbers would be 5 and 125.
Invalid numbers : 1.2, 1.65, 9.5
I am stuck in checking whether a number has a decimal or not.
I tried is_numeric but it accepted numbers with a decimal.
A possibility is to try using strpos:
if (strpos($a,'.') === false) { // or comma can be included as well
echo 'Valid';
}
or try it using regex:
if (preg_match('/^\d+$/',$a))
echo 'Valid';
Examples taken from here.
If you are sure the number variable is not a string you can use is_int() to check whether it is valid or is_float() to check whether it is invalid.
But if you handle forms for example the variable is often a string which makes it harder and this is an easy solution which works on strings, integers and floats:
if (is_numeric($number)) {
//if we already know $number is numeric...
if ((int) $number == $number) {
//is an integer!!
}
}
It's also faster than regex and string methods.
Use is_float() function to find invalid numbers
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 8 years ago.
Improve this question
I have float values in an array... Let's say one of my values is:
5.1234
How do I SWAP the integer in the float. So in the example above, I'd like to swap the 5 with 8. Therefore the new number would be:
8.1234
This needs to be a SWAP, not a mathematical addition as in 5.1234 + 3.
I basically need to split the number in two, the integer (5) and the float value following it (.1234), swap the 5 for the 8 and the recombine them to get 8.1234.
What is the fastest and most elegant way to do this in PHP since I'll be using this on a LOT of data?
To clarify WHY math cannot be used: This is because this is an obj file that's looking for an usemtl library title (Mudbox compliant) from which it extracts the UV space. Then it changes the vert U (or V) accordingly. Problem is these faces may come up more than once. This would make the operation cumulative, which it is NOT. All it needs to do is substituted the integer.
<?php
$number = 5.1234;
$array = explode(".", $number);
// $array[0] contains 5
$newNumber = 8;
$array[0] = $newNumber;
$finalString = $array[0] . '.' . $array[1];
$finalFloat = floatval($finalString); // String to float
echo $finalFloat;
?>
Here is how I would do this. This solution is relevant if you are sure the number will always be formated like followed :
[number].[decimals]
Else you will not be able to always replace the number before the dot.
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 8 years ago.
Improve this question
am using application number generator finction like appilcation number logic:current month.current date count of today.year Ex:09.0801.14
first one am getting fine if start second insert application using my logic it shows 09.08802.14
i found that $getresult it geeting 0801 so am getting 5 digit application number
<?php
$getresult=explode(".",$getresult);
if(!isset($getresult))
{
$ref_num=$getmonth.$getdate.'01'.$getyear;
}
else
{
$getresult=intval($getresult[1])+1;
if($getresult<10)
{
$getresult='0'.$getresult;
}
else
{
$getresult=$getresult;
}
$ref_num=$getmonth.$getdate.$getresult.$getyear;
}
?>
here i need to remove $getresult first two digit number i mean if i get $getresult value 0801 i have change it to 01.
how can i do this
Simple bad but fast fix answer:
echo substr('12345', 0, 4)
Output will be 1234
echo substr('12345', 2, 4);
Output will be 34
Use
$getresult = substr($getresult, 2)
to remove the first two digits.
Try this:
$ref_num=substr($ref_num, 2);
example:
0801 - cuts first 2 letters => 01
try:
$result = substr($string, 2);
api of substr here
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I want to auto increment the alphanumeric character's ID and i want to savi it in to my database.
For example:
Example-001
Example-002
Example-003
Example-004
Example-005
You don't really want to store it like that -- bad idea.
Instead, just have your ID INT AUTO_INCREMENT in your MySQL and do something like
<?php echo "$dbRow['name']."-".$dbRow['id'];
Or, if you're OCD -
while(strlen($dbRow['id'] < 3) {
$dbRow['id'] = "0".$dbRow['id'];
}
If you are looking to increment the number at the end of those strings, and you have to do it in PHP, try this:
$str1 = "Example-001";
$parts = explode("-",$str1);
echo sprintf($parts[0] . '-%03d', $parts[1]+1); // Example-002
However I would guess there's a better way, possibly at the database level to accomplish what you need. You would need to explain more and post more code.
Example: http://3v4l.org/V2R5m
I'm not entirely sure what you mean, but since you tagged this as PHP and you're asking how "to auto increment an alphanumeric character's ID," here's a solution:
for ($i=0; $i<=10; $i++){
$number = str_pad($i, 3, "0", STR_PAD_LEFT);
echo "Example-$number <br />";
}
Outputs:
Example-000
Example-001
Example-002
Example-003
Example-004
Etc...
Not only is does it increment the number, but it does it automatically-ish.
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 9 years ago.
Improve this question
What is the PHP function to get letter (1 char symbol), based on it position.
Like 0 position - a, 1st - b, etc.
I tried this:
"a"+5
I was expecting f but I get 5 instead.
I think you want to use chr() and/or ord() functions. Something like:
echo chr( ord("a") + $i );
$alphabet = range('a', 'z');
echo array_search('b', $alphabet); // 1
Try this chr() and/or ord() functions.
Some like:
print chr( ord("x") + $z );