PHPExcel_Calculation
[ class tree: PHPExcel_Calculation ] [ index: PHPExcel_Calculation ] [ all elements ]

Source for file TextData.php

Documentation is available at TextData.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2011 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20.  *
  21.  * @category    PHPExcel
  22.  * @package        PHPExcel_Calculation
  23.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version        1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  35.     require(PHPEXCEL_ROOT 'PHPExcel/Autoloader.php');
  36. }
  37.  
  38.  
  39. /**
  40.  * PHPExcel_Calculation_TextData
  41.  *
  42.  * @category    PHPExcel
  43.  * @package        PHPExcel_Calculation
  44.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  45.  */
  46.  
  47.     private static $_invalidChars Null;
  48.  
  49.     private static function _uniord($c{
  50.         if (ord($c{0}>=&& ord($c{0}<= 127)
  51.             return ord($c{0});
  52.         if (ord($c{0}>= 192 && ord($c{0}<= 223)
  53.             return (ord($c{0})-192)*64 (ord($c{1})-128);
  54.         if (ord($c{0}>= 224 && ord($c{0}<= 239)
  55.             return (ord($c{0})-224)*4096 (ord($c{1})-128)*64 (ord($c{2})-128);
  56.         if (ord($c{0}>= 240 && ord($c{0}<= 247)
  57.             return (ord($c{0})-240)*262144 (ord($c{1})-128)*4096 (ord($c{2})-128)*64 (ord($c{3})-128);
  58.         if (ord($c{0}>= 248 && ord($c{0}<= 251)
  59.             return (ord($c{0})-248)*16777216 (ord($c{1})-128)*262144 (ord($c{2})-128)*4096 (ord($c{3})-128)*64 (ord($c{4})-128);
  60.         if (ord($c{0}>= 252 && ord($c{0}<= 253)
  61.             return (ord($c{0})-252)*1073741824 (ord($c{1})-128)*16777216 (ord($c{2})-128)*262144 (ord($c{3})-128)*4096 (ord($c{4})-128)*64 (ord($c{5})-128);
  62.         if (ord($c{0}>= 254 && ord($c{0}<= 255//error
  63.             return PHPExcel_Calculation_Functions::VALUE();
  64.         return 0;
  65.     }    //    function _uniord()
  66.  
  67.     /**
  68.      * CHARACTER
  69.      *
  70.      * @param    string    $character    Value
  71.      * @return    int 
  72.      */
  73.     public static function CHARACTER($character{
  74.         $character    PHPExcel_Calculation_Functions::flattenSingleValue($character);
  75.  
  76.         if ((!is_numeric($character)) || ($character 0)) {
  77.             return PHPExcel_Calculation_Functions::VALUE();
  78.         }
  79.  
  80.         if (function_exists('mb_convert_encoding')) {
  81.             return mb_convert_encoding('&#'.intval($character).';''UTF-8''HTML-ENTITIES');
  82.         else {
  83.             return chr(intval($character));
  84.         }
  85.     }
  86.  
  87.  
  88.     /**
  89.      *    TRIMNONPRINTABLE
  90.      *
  91.      *    @param    mixed    $value    Value to check
  92.      *    @return    string 
  93.      */
  94.     public static function TRIMNONPRINTABLE($stringValue ''{
  95.         $stringValue    PHPExcel_Calculation_Functions::flattenSingleValue($stringValue);
  96.  
  97.         if (is_bool($stringValue)) {
  98.             $stringValue ($stringValue'TRUE' 'FALSE';
  99.         }
  100.  
  101.         if (self::$_invalidChars == Null{
  102.             self::$_invalidChars range(chr(0),chr(31));
  103.         }
  104.  
  105.         if (is_string($stringValue|| is_numeric($stringValue)) {
  106.             return str_replace(self::$_invalidChars,'',trim($stringValue,"\x00..\x1F"));
  107.         }
  108.         return Null;
  109.     }    //    function TRIMNONPRINTABLE()
  110.  
  111.  
  112.     /**
  113.      *    TRIMSPACES
  114.      *
  115.      *    @param    mixed    $value    Value to check
  116.      *    @return    string 
  117.      */
  118.     public static function TRIMSPACES($stringValue ''{
  119.         $stringValue    PHPExcel_Calculation_Functions::flattenSingleValue($stringValue);
  120.  
  121.         if (is_string($stringValue|| is_numeric($stringValue)) {
  122.             return trim(preg_replace('/  +/',' ',$stringValue));
  123.         }
  124.         return Null;
  125.     }    //    function TRIMSPACES()
  126.  
  127.  
  128.     /**
  129.      * ASCIICODE
  130.      *
  131.      * @param    string    $character    Value
  132.      * @return    int 
  133.      */
  134.     public static function ASCIICODE($characters{
  135.         $characters    PHPExcel_Calculation_Functions::flattenSingleValue($characters);
  136.         if (is_bool($characters)) {
  137.             if (PHPExcel_Calculation_Functions::getCompatibilityMode(== PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE{
  138.                 $characters = (int) $characters;
  139.             else {
  140.                 if ($characters{
  141.                     $characters 'True';
  142.                 else {
  143.                     $characters 'False';
  144.                 }
  145.             }
  146.         }
  147.  
  148.         $character $characters;
  149.         if ((function_exists('mb_strlen')) && (function_exists('mb_substr'))) {
  150.             if (mb_strlen($characters'UTF-8'1$character mb_substr($characters01'UTF-8')}
  151.             return self::_uniord($character);
  152.         else {
  153.             if (strlen($characters0$character substr($characters01)}
  154.             return ord($character);
  155.         }
  156.     }    //    function ASCIICODE()
  157.  
  158.  
  159.     /**
  160.      * CONCATENATE
  161.      *
  162.      * @return    string 
  163.      */
  164.     public static function CONCATENATE({
  165.         // Return value
  166.         $returnValue '';
  167.  
  168.         // Loop through arguments
  169.         foreach ($aArgs as $arg{
  170.             if (is_bool($arg)) {
  171.                 if (PHPExcel_Calculation_Functions::getCompatibilityMode(== PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE{
  172.                     $arg = (int) $arg;
  173.                 else {
  174.                     if ($arg{
  175.                         $arg 'TRUE';
  176.                     else {
  177.                         $arg 'FALSE';
  178.                     }
  179.                 }
  180.             }
  181.             $returnValue .= $arg;
  182.         }
  183.  
  184.         // Return
  185.         return $returnValue;
  186.     }    //    function CONCATENATE()
  187.  
  188.  
  189.     /**
  190.      *    DOLLAR
  191.      *
  192.      *    This function converts a number to text using currency format, with the decimals rounded to the specified place.
  193.      *    The format used is $#,##0.00_);($#,##0.00)..
  194.      *
  195.      *    @param    float    $value            The value to format
  196.      *    @param    int        $decimals        The number of digits to display to the right of the decimal point.
  197.      *                                     If decimals is negative, number is rounded to the left of the decimal point.
  198.      *                                     If you omit decimals, it is assumed to be 2
  199.      *    @return    string 
  200.      */
  201.     public static function DOLLAR($value 0$decimals 2{
  202.         $value        PHPExcel_Calculation_Functions::flattenSingleValue($value);
  203.         $decimals    is_null($decimalsPHPExcel_Calculation_Functions::flattenSingleValue($decimals);
  204.  
  205.         // Validate parameters
  206.         if (!is_numeric($value|| !is_numeric($decimals)) {
  207.             return PHPExcel_Calculation_Functions::NaN();
  208.         }
  209.         $decimals floor($decimals);
  210.  
  211.         if ($decimals 0{
  212.             return money_format('%.'.$decimals.'n',$value);
  213.         else {
  214.             $round pow(10,abs($decimals));
  215.             if ($value 0$round 0-$round}
  216.             $value PHPExcel_Calculation_MathTrig::MROUND($value,$round);
  217.             //    The implementation of money_format used if the standard PHP function is not available can't handle decimal places of 0,
  218.             //        so we display to 1 dp and chop off that character and the decimal separator using substr
  219.             return substr(money_format('%.1n',$value),0,-2);
  220.         }
  221.     }    //    function DOLLAR()
  222.  
  223.  
  224.     /**
  225.      * SEARCHSENSITIVE
  226.      *
  227.      * @param    string    $needle        The string to look for
  228.      * @param    string    $haystack    The string in which to look
  229.      * @param    int        $offset        Offset within $haystack
  230.      * @return    string 
  231.      */
  232.     public static function SEARCHSENSITIVE($needle,$haystack,$offset=1{
  233.         $needle        PHPExcel_Calculation_Functions::flattenSingleValue($needle);
  234.         $haystack    PHPExcel_Calculation_Functions::flattenSingleValue($haystack);
  235.         $offset        PHPExcel_Calculation_Functions::flattenSingleValue($offset);
  236.  
  237.         if (!is_bool($needle)) {
  238.             if (is_bool($haystack)) {
  239.                 $haystack ($haystack'TRUE' 'FALSE';
  240.             }
  241.  
  242.             if (($offset 0&& (strlen($haystack$offset)) {
  243.                 if (function_exists('mb_strpos')) {
  244.                     $pos mb_strpos($haystack$needle--$offset,'UTF-8');
  245.                 else {
  246.                     $pos strpos($haystack$needle--$offset);
  247.                 }
  248.                 if ($pos !== false{
  249.                     return ++$pos;
  250.                 }
  251.             }
  252.         }
  253.         return PHPExcel_Calculation_Functions::VALUE();
  254.     }    //    function SEARCHSENSITIVE()
  255.  
  256.  
  257.     /**
  258.      * SEARCHINSENSITIVE
  259.      *
  260.      * @param    string    $needle        The string to look for
  261.      * @param    string    $haystack    The string in which to look
  262.      * @param    int        $offset        Offset within $haystack
  263.      * @return    string 
  264.      */
  265.     public static function SEARCHINSENSITIVE($needle,$haystack,$offset=1{
  266.         $needle        PHPExcel_Calculation_Functions::flattenSingleValue($needle);
  267.         $haystack    PHPExcel_Calculation_Functions::flattenSingleValue($haystack);
  268.         $offset        PHPExcel_Calculation_Functions::flattenSingleValue($offset);
  269.  
  270.         if (!is_bool($needle)) {
  271.             if (is_bool($haystack)) {
  272.                 $haystack ($haystack'TRUE' 'FALSE';
  273.             }
  274.  
  275.             if (($offset 0&& (strlen($haystack$offset)) {
  276.                 if (function_exists('mb_stripos')) {
  277.                     $pos mb_stripos($haystack$needle--$offset,'UTF-8');
  278.                 else {
  279.                     $pos stripos($haystack$needle--$offset);
  280.                 }
  281.                 if ($pos !== false{
  282.                     return ++$pos;
  283.                 }
  284.             }
  285.         }
  286.         return PHPExcel_Calculation_Functions::VALUE();
  287.     }    //    function SEARCHINSENSITIVE()
  288.  
  289.  
  290.     /**
  291.      *    FIXEDFORMAT
  292.      *
  293.      *    @param    mixed    $value    Value to check
  294.      *    @return    boolean 
  295.      */
  296.     public static function FIXEDFORMAT($value,$decimals=2,$no_commas=false{
  297.         $value        PHPExcel_Calculation_Functions::flattenSingleValue($value);
  298.         $decimals    PHPExcel_Calculation_Functions::flattenSingleValue($decimals);
  299.         $no_commas        PHPExcel_Calculation_Functions::flattenSingleValue($no_commas);
  300.  
  301.         $valueResult round($value,$decimals);
  302.         if ($decimals 0$decimals 0}
  303.         if (!$no_commas{
  304.             $valueResult number_format($valueResult,$decimals);
  305.         }
  306.  
  307.         return (string) $valueResult;
  308.     }    //    function FIXEDFORMAT()
  309.  
  310.  
  311.     /**
  312.      * LEFT
  313.      *
  314.      * @param    string    $value    Value
  315.      * @param    int        $chars    Number of characters
  316.      * @return    string 
  317.      */
  318.     public static function LEFT($value ''$chars 1{
  319.         $value        PHPExcel_Calculation_Functions::flattenSingleValue($value);
  320.         $chars        PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  321.  
  322.         if ($chars 0{
  323.             return PHPExcel_Calculation_Functions::VALUE();
  324.         }
  325.  
  326.         if (is_bool($value)) {
  327.             $value ($value'TRUE' 'FALSE';
  328.         }
  329.  
  330.         if (function_exists('mb_substr')) {
  331.             return mb_substr($value0$chars'UTF-8');
  332.         else {
  333.             return substr($value0$chars);
  334.         }
  335.     }    //    function LEFT()
  336.  
  337.  
  338.     /**
  339.      *    MID
  340.      *
  341.      *    @param    string    $value    Value
  342.      *    @param    int        $start    Start character
  343.      *    @param    int        $chars    Number of characters
  344.      *    @return    string 
  345.      */
  346.     public static function MID($value ''$start 1$chars null{
  347.         $value        PHPExcel_Calculation_Functions::flattenSingleValue($value);
  348.         $start        PHPExcel_Calculation_Functions::flattenSingleValue($start);
  349.         $chars        PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  350.  
  351.         if (($start 1|| ($chars 0)) {
  352.             return PHPExcel_Calculation_Functions::VALUE();
  353.         }
  354.  
  355.         if (is_bool($value)) {
  356.             $value ($value'TRUE' 'FALSE';
  357.         }
  358.  
  359.         if (function_exists('mb_substr')) {
  360.             return mb_substr($value--$start$chars'UTF-8');
  361.         else {
  362.             return substr($value--$start$chars);
  363.         }
  364.     }    //    function MID()
  365.  
  366.  
  367.     /**
  368.      *    RIGHT
  369.      *
  370.      *    @param    string    $value    Value
  371.      *    @param    int        $chars    Number of characters
  372.      *    @return    string 
  373.      */
  374.     public static function RIGHT($value ''$chars 1{
  375.         $value        PHPExcel_Calculation_Functions::flattenSingleValue($value);
  376.         $chars        PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  377.  
  378.         if ($chars 0{
  379.             return PHPExcel_Calculation_Functions::VALUE();
  380.         }
  381.  
  382.         if (is_bool($value)) {
  383.             $value ($value'TRUE' 'FALSE';
  384.         }
  385.  
  386.         if ((function_exists('mb_substr')) && (function_exists('mb_strlen'))) {
  387.             return mb_substr($valuemb_strlen($value'UTF-8'$chars$chars'UTF-8');
  388.         else {
  389.             return substr($valuestrlen($value$chars);
  390.         }
  391.     }    //    function RIGHT()
  392.  
  393.  
  394.     /**
  395.      * STRINGLENGTH
  396.      *
  397.      * @param    string    $value    Value
  398.      * @param    int        $chars    Number of characters
  399.      * @return    string 
  400.      */
  401.     public static function STRINGLENGTH($value ''{
  402.         $value        PHPExcel_Calculation_Functions::flattenSingleValue($value);
  403.  
  404.         if (is_bool($value)) {
  405.             $value ($value'TRUE' 'FALSE';
  406.         }
  407.  
  408.         if (function_exists('mb_strlen')) {
  409.             return mb_strlen($value'UTF-8');
  410.         else {
  411.             return strlen($value);
  412.         }
  413.     }    //    function STRINGLENGTH()
  414.  
  415.  
  416.     /**
  417.      *    LOWERCASE
  418.      *
  419.      *    Converts a string value to upper case.
  420.      *
  421.      *    @param    string        $mixedCaseString 
  422.      *    @return    string 
  423.      */
  424.     public static function LOWERCASE($mixedCaseString{
  425.         $mixedCaseString    PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString);
  426.  
  427.         if (is_bool($mixedCaseString)) {
  428.             $mixedCaseString ($mixedCaseString'TRUE' 'FALSE';
  429.         }
  430.  
  431.         if (function_exists('mb_convert_case')) {
  432.             return mb_convert_case($mixedCaseStringMB_CASE_LOWER'UTF-8');
  433.         else {
  434.             return strtoupper($mixedCaseString);
  435.         }
  436.     }    //    function LOWERCASE()
  437.  
  438.  
  439.     /**
  440.      *    UPPERCASE
  441.      *
  442.      *    Converts a string value to upper case.
  443.      *
  444.      *    @param    string        $mixedCaseString 
  445.      *    @return    string 
  446.      */
  447.     public static function UPPERCASE($mixedCaseString{
  448.         $mixedCaseString    PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString);
  449.  
  450.         if (is_bool($mixedCaseString)) {
  451.             $mixedCaseString ($mixedCaseString'TRUE' 'FALSE';
  452.         }
  453.  
  454.         if (function_exists('mb_convert_case')) {
  455.             return mb_convert_case($mixedCaseStringMB_CASE_UPPER'UTF-8');
  456.         else {
  457.             return strtoupper($mixedCaseString);
  458.         }
  459.     }    //    function UPPERCASE()
  460.  
  461.  
  462.     /**
  463.      *    PROPERCASE
  464.      *
  465.      *    Converts a string value to upper case.
  466.      *
  467.      *    @param    string        $mixedCaseString 
  468.      *    @return    string 
  469.      */
  470.     public static function PROPERCASE($mixedCaseString{
  471.         $mixedCaseString    PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString);
  472.  
  473.         if (is_bool($mixedCaseString)) {
  474.             $mixedCaseString ($mixedCaseString'TRUE' 'FALSE';
  475.         }
  476.  
  477.         if (function_exists('mb_convert_case')) {
  478.             return mb_convert_case($mixedCaseStringMB_CASE_TITLE'UTF-8');
  479.         else {
  480.             return ucwords($mixedCaseString);
  481.         }
  482.     }    //    function PROPERCASE()
  483.  
  484.  
  485.     /**
  486.      *    REPLACE
  487.      *
  488.      *    @param    string    $value    Value
  489.      *    @param    int        $start    Start character
  490.      *    @param    int        $chars    Number of characters
  491.      *    @return    string 
  492.      */
  493.     public static function REPLACE($oldText ''$start 1$chars null$newText{
  494.         $oldText    PHPExcel_Calculation_Functions::flattenSingleValue($oldText);
  495.         $start        PHPExcel_Calculation_Functions::flattenSingleValue($start);
  496.         $chars        PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  497.         $newText    PHPExcel_Calculation_Functions::flattenSingleValue($newText);
  498.  
  499.         $left self::LEFT($oldText,$start-1);
  500.         $right self::RIGHT($oldText,self::STRINGLENGTH($oldText)-($start+$chars)+1);
  501.  
  502.         return $left.$newText.$right;
  503.     }    //    function REPLACE()
  504.  
  505.  
  506.     /**
  507.      *    SUBSTITUTE
  508.      *
  509.      *    @param    string    $text        Value
  510.      *    @param    string    $fromText    From Value
  511.      *    @param    string    $toText        To Value
  512.      *    @param    integer    $instance    Instance Number
  513.      *    @return    string 
  514.      */
  515.     public static function SUBSTITUTE($text ''$fromText ''$toText ''$instance 0{
  516.         $text        PHPExcel_Calculation_Functions::flattenSingleValue($text);
  517.         $fromText    PHPExcel_Calculation_Functions::flattenSingleValue($fromText);
  518.         $toText        PHPExcel_Calculation_Functions::flattenSingleValue($toText);
  519.         $instance    floor(PHPExcel_Calculation_Functions::flattenSingleValue($instance));
  520.  
  521.         if ($instance == 0{
  522.             if(function_exists('mb_str_replace')) {
  523.                 return mb_str_replace($fromText,$toText,$text);
  524.             else {
  525.                 return str_replace($fromText,$toText,$text);
  526.             }
  527.         else {
  528.             $pos = -1;
  529.             while($instance 0{
  530.                 if (function_exists('mb_strpos')) {
  531.                     $pos mb_strpos($text$fromText$pos+1'UTF-8');
  532.                 else {
  533.                     $pos strpos($text$fromText$pos+1);
  534.                 }
  535.                 if ($pos === false{
  536.                     break;
  537.                 }
  538.                 --$instance;
  539.             }
  540.             if ($pos !== false{
  541.                 if (function_exists('mb_strlen')) {
  542.                     return self::REPLACE($text,++$pos,mb_strlen($fromText'UTF-8'),$toText);
  543.                 else {
  544.                     return self::REPLACE($text,++$pos,strlen($fromText),$toText);
  545.                 }
  546.             }
  547.         }
  548.  
  549.         return $left.$newText.$right;
  550.     }    //    function SUBSTITUTE()
  551.  
  552.  
  553.     /**
  554.      *    RETURNSTRING
  555.      *
  556.      *    @param    mixed    $value    Value to check
  557.      *    @return    boolean 
  558.      */
  559.     public static function RETURNSTRING($testValue ''{
  560.         $testValue    PHPExcel_Calculation_Functions::flattenSingleValue($testValue);
  561.  
  562.         if (is_string($testValue)) {
  563.             return $testValue;
  564.         }
  565.         return Null;
  566.     }    //    function RETURNSTRING()
  567.  
  568.  
  569.     /**
  570.      *    TEXTFORMAT
  571.      *
  572.      *    @param    mixed    $value    Value to check
  573.      *    @return    boolean 
  574.      */
  575.     public static function TEXTFORMAT($value,$format{
  576.         $value    PHPExcel_Calculation_Functions::flattenSingleValue($value);
  577.         $format    PHPExcel_Calculation_Functions::flattenSingleValue($format);
  578.  
  579.         if ((is_string($value)) && (!is_numeric($value)) && PHPExcel_Shared_Date::isDateTimeFormatCode($format)) {
  580.             $value PHPExcel_Calculation_DateTime::DATEVALUE($value);
  581.         }
  582.  
  583.         return (string) PHPExcel_Style_NumberFormat::toFormattedString($value,$format);
  584.     }    //    function TEXTFORMAT()
  585.  
  586. }    //    class PHPExcel_Calculation_TextData

Documentation generated on Sun, 27 Feb 2011 16:35:38 -0800 by phpDocumentor 1.4.3