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 2 years ago.
Improve this question
I need second last number from the list and the list can have varied length. I tried seperating it using explode(",", $layouts);
$layouts= '1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,14,0,15,0,16,0,17,0,18,0,19,0,20,0,21,0,22,0,23,0,24,0,25,0,26,0,27,0,28,0,29,0,30,0';
Example : I need to get 30 from the string here.
please help
If this list can grow in size as you have indicated in the comment, then usually explode it into an array, then do a count of the size, subtract two from the total size and use that as an index to get the value:
//turn this to array
$layouts = '1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,14,0,15,0,16,0,17,0,18,0,19,0,20,0,21,0,22,0,23,0,24,0,25,0,26,0,27,0,28,0,29,0,30,0';
$layoutsArray = explode(",", $layouts);
$xtarget = count($layoutsArray) - 2;
$xvalue = "";
if($xtarget > -1){
$xvalue = $layoutsArray[$xtarget];
}else {
//it could be wrong
}
You can explode it with , and get 2nd last value from array try the following code
$layouts= '1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,14,0,15,0,16,0,17,0,18,0,19,0,20,0,21,0,22,0,23,0,24,0,25,0,26,0,27,0,28,0,29,0,30,0';
$array = explode(',',$layouts);
if(count($array) > 2){
$pair = array_slice($array, -2, 1, true);
$key = key($pair);
$value = current($pair);
echo $value;
}
Output
30
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
so I want to add two values (money values) but can't quite work it out.
I keep getting a notice, I've tried number_format to format it but even using that I get the 'Notice: A non well formed numeric value encountered'
$price = 0.00;
while($row = $result->fetch_assoc()) {
$dbprice = $row["ProductSellPrice"];
$price = $price + $dbprice;
//echo number_format($price + $dbprice,2);
}
however I get this problem: Notice: A non well formed numeric value encountered
If I add two values '5' + '1.5' I get '6' as a result.
Edit:
The values I were pulling from the database were formatted with commas.
PHP numbers uses dot as separator and seems that you have coma. Please replace the coma with the dot as first. After that you can cast the prices into floats and finally sum it up :)
You can try with this (based on you example):
<?php
$price = 0.00;
while($row = $result->fetch_assoc()) {
$dbprice = $row["ProductSellPrice"];
// Replace comma. Check also for possible thousand separator.
$dbprice = str_replace(',', '.', $dbprice);
$price = $price + (float)$dbprice;
//echo number_format($price + $dbprice, 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 4 years ago.
Improve this question
I am trying to write a function that takes the following 2 parameters:
A sentence as a string
A number of lines as an integer
So if I was to call formatLines("My name is Gary", 2); ...
The possible outcomes would be:
array("My name is", "Gary");
array("My name", "is Gary");
array("My", "name is Gary");
It would return: array("My name", "is Gary"); because the difference in character counts for each line is as small as possible.
So the part I am ultimately stuck on is creating an array of possible outcomes where the words are in the correct order, split over x lines. Once I have an array of possible outcomes I would be fine working out the best result.
So how would I go about generating all the possible combinations?
Regards
Joe
It seems like doing this by creating all possible ways of splitting the text and then determining the best one would be unnecessarily inefficient. You can count the characters and divide by the number of lines to find approximately the right number of characters per line.
function lineSplitChars($text, $lines) {
if (str_word_count($text) < $lines) {
throw new InvalidArgumentException('lines must be fewer than word count', 1);
}
$width = strlen($text) / $lines; // initial width calculation
while ($width > 0) {
$result = explode("\n", wordwrap($text, $width)); // generate result
// check for correct number of lines. return if correct, adjust width if not
$n = count($result);
if ($n == $lines) return $result;
if ($n > $lines) {
$width++;
} else {
$width--;
};
}
}
An answer has been accepted here - but this strikes me as a rather cumbersome method for solving the problem when PHP already provides a wordwrap() function which does most of the heavy lifting:
function format_lines($str, $lines)
{
$guess_length=(integer)(strlen($str)/($lines+1));
do {
$out=explode("\n", wordwrap($str, $guess_length));
$guess_length++;
} while ($guess_length<strlen($str) && count($out)>$lines);
return $out;
}
As it stands, it is rather a brute force method, and for very large inputs, a better solution would use optimum searching (adding/removing a larger initial interval then decreasing this in iterations)
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 7 years ago.
Improve this question
I am just trying to figure out how I can get the actual digits of a figure that has been calculated within php and formatted to have 2 decimal places.
so say its calculated it to be 45.76 I am trying to figure out how I can get the 76 from it for an if statement. Basically I want it to look and just say that if it's 00 then remove them, if not, show them.
Thanks
Try :
function showDecimals($v){
$n = abs($v);
$whole = floor($n);
$fraction = $n - $whole;
return $fraction > 0
}
And...
if (showDecimals(10.15)){
//Show
}else{
//Remove?
}
You want to show a whole number if there is no decimal place, and 2 decimals of precision if not?
Method 1
function formatNumber($n) {
$n = round($n*100)/100;
return ''+$n;
}
This simply rounds it to 2 decimals of precision. Zero truncation is automatic.
Usage
echo formatNumber(0); //0
echo formatNumber(0.5); //0.5
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.9
echo formatNumber(1.896); //1.9
Method 2
Or if you 1.9 to display as 1.90, I suppose this would work:
function formatNumber($n) {
if ($n == 0)
return ''+$n;
$str = ''.round($n*100)/100;
$dotpos = strrpos('.', $str);
if (strlen(substr($str, $dotpos+1)) === 2)
$str .= '0';
return $str;
}
Usage:
echo formatNumber(0); //0
echo formatNumber(0.5); //0.50
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.90
echo formatNumber(1.896); //1.90
Edit: Accidentally posted broken version of method 2, should be fixed now.
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