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.
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
I'm trying to find a way to turn convert 36.901775282237 to 36.91 in php which function do I use in order to convert to this value. I've tried the round function but it's not giving me the right value no matter what flag I use.
The normal way is to use number_format
<?php
$newvalue=number_format(36.901775282237,2);
echo $newvalue;
?>
But if you wish to do what you want , then use a few lines of codes. (I think no need to further explain, just simple math)
<?php
$value=36.901775282237;
//$value=36.900775282237; for this case the result will be 36.90
///// do the trick
$value=intval($value * 1000)/1000;
$parsex=number_format($value * 100,2, ".", "");
$parsex=ceil($parsex)/100;
//// end
echo $parsex;
?>
Mathematically speaking, 36.901775282237 doesn't round up to 36.91 but rather rounds down to 36.90. You'll need to combine ceil with some custom math to get the rounding you're asking for. If you're looking for mathematically correct rounding, number_format will accomplish that internally:
function ceilWithPercision($number, $decimalPoint) {
$number = $number * pow(10, $decimalPoint);
$number = ceil($number);
$number = $number / pow(10, $decimalPoint);
return (string) $number;
}
$number = 36.901775282237;
var_dump(ceilWithPercision($number, 2)); // "36.91"
var_dump(number_format($number, 2)); // "36.90"
Example here: https://3v4l.org/NMOpH
You should take a look at some of the Php Documentation, and maybe some tutorials online before asking these kinds of questions here.
What I think you are looking for is number_format(36.901775282237, 2)
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
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 need some help in PHP which ignore the word after 500 character count.
This is my PHP code :
if(str_word_count($rsa['content']) > 500){
}
I will push this content inside an array and the array already inside a loop. I just no idea how to ignore the word after 500 character count.
can some one help me to solve it? Thanks in advanced.
Easy. Use substr.
$content = substr($rsa['content'], 0, 500);
The way that works is substr is the function, $rsa['content'] is the string value, 0 is where substr should start & 500 is the string length you wish to have returned.
That said, it seems you are using str_word_count which counts words when you mean to use strlen instead since that counts characters. Knowing that, I would implement it like this. T :
// Assign the value of `$rsa['content']` to `$content`.
$content = $rsa['content'];
// Set a `$content_length` to save yourself typing & logic headaches.
$content_length = 500;
// Check the string length & act on it.
if (strlen($content) > $content_length) {
$content = substr($content, 0, $content_length);
}
But you could probably make it even slicker by using a ternary operator instead like so:
// Check the string length & act on it.
$content = strlen($content) > $content_length ? substr($content, 0, $content_length) : $content;
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 );
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
I was just wondering if it was possible to add random characters to the variable I am passing to the second page. I want this because if the user changes the value in the url, then the system is gonna mess up because I am inserting data to database based on the message id. I can't use session because the first session is overriding the others.
If I have something like view_inbox.php?messageid=2 then the user can change it to something view_inbox.php?message=4.
So is it possible to have some random characters like
view_inbox.php?messageid=GXLSsd2sdcds? The id is coming from database.
echo"<a href='view_inbox.php?messageid=".$row['id']."'>".$row['from_user']."</a>";
view_inbox.php
$id = $_GET['messageid'];
There are a couple of approaches.
You should be checking security rules on which rows/entities the user is allowed to access. Put these rules in a common procedure/function in your code, so you can check them consistently.
You can also "obfuscate" or encrypt the ID, in a way the server can reverse but is not easy/obvious for the client. Operations could include multiplying by a prime number (say 23) modulo 2^32, XOR by a constant, outputting it in base-64, perhaps with a lowercase 'x' in front.
For the second approach:
function encodeKey ($key) {
$multiplied = $key * 23;
$packed = pack( "N", $multiplied);
$base64 = base64_encode( $packed);
return $base64;
}
function decodeKey ($text) {
$packed = base64_decode( $text);
// then unpack, divide etc.
return $key;
}