Im trying print Excel file data on a page. To do it i used PHPExcel lib, all works good, beside printing formulas, i have simple example with such formula =SUM(C2:C5)
I print values in a such way:
$val = $cell->getValue();
echo '<td>' . $val . '</td>';
how can i check if $val is a formula?
PHPExcel_Cell_DataType::dataTypeForValue($val); told me that it is a just another one string in my $val
Ofc i can calculate it in a loop, and chek if it`s a last row - insert needed info by hands, but how i can calculate it easy way?
Will be pleased to hear your advice. Thanks.
PHPExcel_Cell_DataType::dataTypeForValue($val); will always tell you string for a formula, because a formula is a string. Being a formula is related to the cell, not the data. The getDataType() method of the cell object will return an 'f' for formula.
If you use getCalculatedValue() rather than getValue(), then PHPExcel will determine whether the cell contains a formula or not. If it does, then it will calculate the result and return that, otherwise it will simply return whatever would have been returned by getValue(); The other method you might consider is getFormattedValue() which will return a string, formatted according to whatever rules are set for the cell. And if it was a formula cell, then it will have done the calculation as well. Particularly useful to get a date string rather than a numeric value from cells formatted as dates.
You can also avoid looping of the cells by using the toArray() or rangeToArray() methods, which will return an array of cell values, though you'd still need to loop the array. Both of these methods have arguments allowing you to set whether formulae should be calculated, and whether formatting should be applied.
"how can i check if $val is a formula?"
Every formula in excel start with = so :
<?php
$String = 'SUM(B3:B8)';
# See http://www.php.net/manual/en/language.types.string.php
# $String{} is deprecated as of PHP 6.
if($String[0] == '='){
echo 'Yes';
} else {
echo 'No';
}
?>
OR
<?php
function IsExcelFormula($String=null){
if(!$String OR strlen($String) <= 0){
return false;
}
$First = $String[0];
return ($First == '=' ? true : false);
}
var_dump(IsExcelFormula('=SUM(B8:B9)')); // bool(true)
var_dump(IsExcelFormula('TOTAL(B3:B6)')); // bool(false)
?>
Related
I have a bunch of text in a database, which is id'ed by "grp" how would i get to bind each text to "example :
if($grp->$grp === '1') {
---> $text+$grp->$grp <--- = $grp->text
}
Is it possible to do a foreach & while loop, that binds the text to each "text1, text2 etc" ?
I've tried this way
foreach ($getgrouptxt as $grp) {
$grpnr = $grp->grp;
while ($grpnr < '63') {
$text+$grpnr = $grp->text;}
}
}
So basicly
Getting text from an array, using foreach looping through everyone of them
I want it to set the text var ($text"number") to
$grp-text, each time it hits a number until 62 is reached
+ is for numeric addition
You intend to append a number to a string, but you are using the numeric addition operator (+) instead of string concatenation operator (.). So, instead of $text+$grpnr you need $text.$grpnr
If you want to assign a value, the variable which will hold it is at the left-hand side of the assignment
If you want to store $text.$grpnr into $grp->text, then you need to invert the assignment:
$grp->text = $text.$grpnr;
If you want to use a dynamic variable name, then use {} around it
If $text.$grpnr should be a variable name and you intend to store $grp->text into it, then:
{$text.$grpnr} = $grp->text;
This will not work in PHP 5 though.
Beware comparing numbers as strings
'7' < '63'
is false, because PHP will do a textual comparison and '7' is behind '6' in the alphabet. If you want to use numeric comparisons, compare to 63 and convert $grpnr to a numeric type if it is textual.
Suggestion
According to the best of my understanding of your problem, you need something like
foreach ($getgrouptxt as $grp) {
$grpnr = $grp->grp;
while (((int)$grpnr) < 63) {
$grp->text = $text.$grpnr;
}
}
I read some data from a csv file. Each line in the file has a float value. these values can be either:
.123 : starting with a period, so I need to add a zero before.
1,23: having a delimter comma ',' instead of period so I need to change that.
1.2e3 having an exponential-format so I need to convert it to decimal format.
I can't use the function number_format because I can't set the number of decimal points (the float numbers don't have a fixed length of the decimal part and we want to take them as they are to not lose data).
Here is what I tried so far; I built two functions, the first one filters the floats, the second one corrects them when the filter returns false:
function validateFloat($float){
if(!filter_var($float,FILTER_VALIDATE_FLOAT,array('flags' => FILTER_FLAG_ALLOW_FRACTION))){
return false;
}
}
function correctFloat($float){
if (validateFloat($float)==false){
$number = number_format($float,null,'.');
str_replace($number,'',$line);
}
}
I don't know how to build the correctFloat function. Any suggestions ? Appreciate it.
Your function can check if there is a comma and get the correct deliminator then use floarval on any other case
function change_format($value){
if(is_string($value)){
//has to be a string if using ','
$value= str_replace(",",".",$value);
}
return floatval($value);
}
echo change_format(.123) ."<br>";
echo change_format("1,23") ."<br>";
echo change_format("1.2e3");
Outputs:
0.123
1.23
1200
I'm trying to get the fractional number count of a decimal number. I tried following simple code, but the loop runs forever.
$var = 123.456;
$fraction_count = 0;
echo $var, "<br>";
while (is_double($var)) {
$var *= 10;
$fraction_count++;
echo $var, "<br>";
}
I'm new to php so, forgive me if this is a stupid question!
Floating point numbers are not trivial to deal with (see the manual for more information). But you don't even need to deal with the number as such.
If you treat it as a string, you can explode() on the . and use strlen() on the second part to get the $fraction_count:
$var = 123.456;
$parts = explode('.', $var);
$fraction_count = strlen($parts[1]);
Since PHP doesn't save trailing 0s on numbers, this will work for 123.4560 as well, but be sure to input it as a number, "123.4560" will get you a wrong result.
I am tring this method to find the common characters in two strings namely, $a and $r, but the first character isn't getting printed . Moreover the $already collects the common characters and prevents them from being printed for multiple times( I need each character to be printed once only) but it isn't doing so. Please tell me what errors I am making.
<?php
$a="BNJUBCI CBDIDIBO";
$r="SBKJOJLBOU";
$already="";
for($i=0;$i<strlen($r);$i++)
{
if (stripos($a,$r[$i])!=FALSE)
{
if (stripos($already,$r[$i])==FALSE)
{
$already=$already.$r[$i];
echo "already=".$already."<br>";
echo $r[$i]."<br>";
}
}
}
?>
Use !==FALSE instead of !=FALSE. The problem is that stripos returns 0 if the needle is at the start of the haystack, and 0 is falsy. By using !== you are forcing it to ensure the result is actually false, and not just 0.
This is actually listed in the docs. An "RTM" might be appropriate here.
Warning
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
The simplest way to find the intersection of the two strings in PHP is to turn them into arrays and use the built-in functions for that purpose.
The following will show all the unique and common characters between the two strings.
<?php
$a="BNJUBCI CBDIDIBO";
$r="SBKJOJLBOU";
$a_arr = str_split($a);
$r_arr = str_split($r);
$common = implode(array_unique(array_intersect($a_arr, $r_arr)));
echo "'$common'";
?>
I would think a much simpler solution to this would be to make the strings into arrays and compare those no?
Something like:
<?php
$a="BNJUBCI CBDIDIBO";
$r="SBKJOJLBOU";
$shared = implode( '' , array_intersect( str_split($a) , str_split($r) ) );
?>
That should return you a string of all the characters in $a that are present in $r
I have a variable that is...
$whatever = "5865/100";
This is a text variable.
I want it to calculate 5865/100 , so that I can add it to other numbers and do a calculation.
Number_format doesn't work, as it just returns "5,865". Whereas I want it to return 58.65
I could do...
$explode=explode("/",$whatever);
if(count($explode)=="2") {
$whatever = $explode[0]/$explode[1];
}
But it seems rather messy. Is there a simpler way?
Evaluate as PHP expression, but first check if it contains only digits and operators and space, and suppress any errors.
if (preg_match('/^[\d\+\-\/\*\s]+$/', $s)) {
#eval('$result = ' . $s . ';');
}
You can use the eval function to evaluate a string as code. However, you have to be careful as to where this code comes from because it will execute anything passed to it, not just simple math. If you knew your string contained a mathematical formula, you could do the following
$answer = 0;
$whatever = "5865/100";
eval ('$answer = ' . $whatever . ';');
print($answer);