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

Source for file String.php

Documentation is available at String.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2010 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_Shared
  23.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.3c, 2010-06-01
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Shared_String
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Shared
  34.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. {
  37.     /**    Constants                */
  38.             const STRING_REGEXP_FRACTION    '(-?)(\d+)\s+(\d+\/\d+)';
  39.  
  40.  
  41.     /**
  42.      * Control characters array
  43.      *
  44.      * @var string[] 
  45.      */
  46.     private static $_controlCharacters array();
  47.  
  48.     /**
  49.      * SYLK Characters array
  50.      *
  51.      * $var array
  52.      */
  53.     private static $_SYLKCharacters array();
  54.  
  55.     /**
  56.      * Decimal separator
  57.      *
  58.      * @var string 
  59.      */
  60.     private static $_decimalSeparator;
  61.  
  62.     /**
  63.      * Thousands separator
  64.      *
  65.      * @var string 
  66.      */
  67.     private static $_thousandsSeparator;
  68.  
  69.     /**
  70.      * Is mbstring extension avalable?
  71.      *
  72.      * @var boolean 
  73.      */
  74.     private static $_isMbstringEnabled;
  75.  
  76.     /**
  77.      * Is iconv extension avalable?
  78.      *
  79.      * @var boolean 
  80.      */
  81.     private static $_isIconvEnabled;
  82.  
  83.     /**
  84.      * Build control characters array
  85.      */
  86.     private static function _buildControlCharacters({
  87.         for ($i 0$i <= 31++$i{
  88.             if ($i != && $i != 10 && $i != 13{
  89.                 $find '_x' sprintf('%04s' strtoupper(dechex($i))) '_';
  90.                 $replace chr($i);
  91.                 self::$_controlCharacters[$find$replace;
  92.             }
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Build SYLK characters array
  98.      */
  99.     private static function _buildSYLKCharacters()
  100.     {
  101.         self::$_SYLKCharacters array(
  102.             "\x1B 0"  => chr(0),
  103.             "\x1B 1"  => chr(1),
  104.             "\x1B 2"  => chr(2),
  105.             "\x1B 3"  => chr(3),
  106.             "\x1B 4"  => chr(4),
  107.             "\x1B 5"  => chr(5),
  108.             "\x1B 6"  => chr(6),
  109.             "\x1B 7"  => chr(7),
  110.             "\x1B 8"  => chr(8),
  111.             "\x1B 9"  => chr(9),
  112.             "\x1B :"  => chr(10),
  113.             "\x1B ;"  => chr(11),
  114.             "\x1B <"  => chr(12),
  115.             "\x1B :"  => chr(13),
  116.             "\x1B >"  => chr(14),
  117.             "\x1B ?"  => chr(15),
  118.             "\x1B!0"  => chr(16),
  119.             "\x1B!1"  => chr(17),
  120.             "\x1B!2"  => chr(18),
  121.             "\x1B!3"  => chr(19),
  122.             "\x1B!4"  => chr(20),
  123.             "\x1B!5"  => chr(21),
  124.             "\x1B!6"  => chr(22),
  125.             "\x1B!7"  => chr(23),
  126.             "\x1B!8"  => chr(24),
  127.             "\x1B!9"  => chr(25),
  128.             "\x1B!:"  => chr(26),
  129.             "\x1B!;"  => chr(27),
  130.             "\x1B!<"  => chr(28),
  131.             "\x1B!="  => chr(29),
  132.             "\x1B!>"  => chr(30),
  133.             "\x1B!?"  => chr(31),
  134.             "\x1B'?"  => chr(127),
  135.             "\x1B(0"  => '€'// 128 in CP1252
  136.             "\x1B(2"  => '‚'// 130 in CP1252
  137.             "\x1B(3"  => 'ƒ'// 131 in CP1252
  138.             "\x1B(4"  => '„'// 132 in CP1252
  139.             "\x1B(5"  => '…'// 133 in CP1252
  140.             "\x1B(6"  => '†'// 134 in CP1252
  141.             "\x1B(7"  => '‡'// 135 in CP1252
  142.             "\x1B(8"  => 'ˆ'// 136 in CP1252
  143.             "\x1B(9"  => '‰'// 137 in CP1252
  144.             "\x1B(:"  => 'Š'// 138 in CP1252
  145.             "\x1B(;"  => '‹'// 139 in CP1252
  146.             "\x1BNj"  => 'Œ'// 140 in CP1252
  147.             "\x1B(>"  => 'Ž'// 142 in CP1252
  148.             "\x1B)1"  => '‘'// 145 in CP1252
  149.             "\x1B)2"  => '’'// 146 in CP1252
  150.             "\x1B)3"  => '“'// 147 in CP1252
  151.             "\x1B)4"  => '”'// 148 in CP1252
  152.             "\x1B)5"  => '•'// 149 in CP1252
  153.             "\x1B)6"  => '–'// 150 in CP1252
  154.             "\x1B)7"  => '—'// 151 in CP1252
  155.             "\x1B)8"  => '˜'// 152 in CP1252
  156.             "\x1B)9"  => '™'// 153 in CP1252
  157.             "\x1B):"  => 'š'// 154 in CP1252
  158.             "\x1B);"  => '›'// 155 in CP1252
  159.             "\x1BNz"  => 'œ'// 156 in CP1252
  160.             "\x1B)>"  => 'ž'// 158 in CP1252
  161.             "\x1B)?"  => 'Ÿ'// 159 in CP1252
  162.             "\x1B*0"  => ' '// 160 in CP1252
  163.             "\x1BN!"  => '¡'// 161 in CP1252
  164.             "\x1BN\"" => '¢'// 162 in CP1252
  165.             "\x1BN#"  => '£'// 163 in CP1252
  166.             "\x1BN("  => '¤'// 164 in CP1252
  167.             "\x1BN%"  => '¥'// 165 in CP1252
  168.             "\x1B*6"  => '¦'// 166 in CP1252
  169.             "\x1BN'"  => '§'// 167 in CP1252
  170.             "\x1BNH " => '¨'// 168 in CP1252
  171.             "\x1BNS"  => '©'// 169 in CP1252
  172.             "\x1BNc"  => 'ª'// 170 in CP1252
  173.             "\x1BN+"  => '«'// 171 in CP1252
  174.             "\x1B*<"  => '¬'// 172 in CP1252
  175.             "\x1B*="  => '­'// 173 in CP1252
  176.             "\x1BNR"  => '®'// 174 in CP1252
  177.             "\x1B*?"  => '¯'// 175 in CP1252
  178.             "\x1BN0"  => '°'// 176 in CP1252
  179.             "\x1BN1"  => '±'// 177 in CP1252
  180.             "\x1BN2"  => '²'// 178 in CP1252
  181.             "\x1BN3"  => '³'// 179 in CP1252
  182.             "\x1BNB " => '´'// 180 in CP1252
  183.             "\x1BN5"  => 'µ'// 181 in CP1252
  184.             "\x1BN6"  => '¶'// 182 in CP1252
  185.             "\x1BN7"  => '·'// 183 in CP1252
  186.             "\x1B+8"  => '¸'// 184 in CP1252
  187.             "\x1BNQ"  => '¹'// 185 in CP1252
  188.             "\x1BNk"  => 'º'// 186 in CP1252
  189.             "\x1BN;"  => '»'// 187 in CP1252
  190.             "\x1BN<"  => '¼'// 188 in CP1252
  191.             "\x1BN="  => '½'// 189 in CP1252
  192.             "\x1BN>"  => '¾'// 190 in CP1252
  193.             "\x1BN?"  => '¿'// 191 in CP1252
  194.             "\x1BNAA" => 'À'// 192 in CP1252
  195.             "\x1BNBA" => 'Á'// 193 in CP1252
  196.             "\x1BNCA" => 'Â'// 194 in CP1252
  197.             "\x1BNDA" => 'Ã'// 195 in CP1252
  198.             "\x1BNHA" => 'Ä'// 196 in CP1252
  199.             "\x1BNJA" => 'Å'// 197 in CP1252
  200.             "\x1BNa"  => 'Æ'// 198 in CP1252
  201.             "\x1BNKC" => 'Ç'// 199 in CP1252
  202.             "\x1BNAE" => 'È'// 200 in CP1252
  203.             "\x1BNBE" => 'É'// 201 in CP1252
  204.             "\x1BNCE" => 'Ê'// 202 in CP1252
  205.             "\x1BNHE" => 'Ë'// 203 in CP1252
  206.             "\x1BNAI" => 'Ì'// 204 in CP1252
  207.             "\x1BNBI" => 'Í'// 205 in CP1252
  208.             "\x1BNCI" => 'Î'// 206 in CP1252
  209.             "\x1BNHI" => 'Ï'// 207 in CP1252
  210.             "\x1BNb"  => 'Ð'// 208 in CP1252
  211.             "\x1BNDN" => 'Ñ'// 209 in CP1252
  212.             "\x1BNAO" => 'Ò'// 210 in CP1252
  213.             "\x1BNBO" => 'Ó'// 211 in CP1252
  214.             "\x1BNCO" => 'Ô'// 212 in CP1252
  215.             "\x1BNDO" => 'Õ'// 213 in CP1252
  216.             "\x1BNHO" => 'Ö'// 214 in CP1252
  217.             "\x1B-7"  => '×'// 215 in CP1252
  218.             "\x1BNi"  => 'Ø'// 216 in CP1252
  219.             "\x1BNAU" => 'Ù'// 217 in CP1252
  220.             "\x1BNBU" => 'Ú'// 218 in CP1252
  221.             "\x1BNCU" => 'Û'// 219 in CP1252
  222.             "\x1BNHU" => 'Ü'// 220 in CP1252
  223.             "\x1B-="  => 'Ý'// 221 in CP1252
  224.             "\x1BNl"  => 'Þ'// 222 in CP1252
  225.             "\x1BN{"  => 'ß'// 223 in CP1252
  226.             "\x1BNAa" => 'à'// 224 in CP1252
  227.             "\x1BNBa" => 'á'// 225 in CP1252
  228.             "\x1BNCa" => 'â'// 226 in CP1252
  229.             "\x1BNDa" => 'ã'// 227 in CP1252
  230.             "\x1BNHa" => 'ä'// 228 in CP1252
  231.             "\x1BNJa" => 'å'// 229 in CP1252
  232.             "\x1BNq"  => 'æ'// 230 in CP1252
  233.             "\x1BNKc" => 'ç'// 231 in CP1252
  234.             "\x1BNAe" => 'è'// 232 in CP1252
  235.             "\x1BNBe" => 'é'// 233 in CP1252
  236.             "\x1BNCe" => 'ê'// 234 in CP1252
  237.             "\x1BNHe" => 'ë'// 235 in CP1252
  238.             "\x1BNAi" => 'ì'// 236 in CP1252
  239.             "\x1BNBi" => 'í'// 237 in CP1252
  240.             "\x1BNCi" => 'î'// 238 in CP1252
  241.             "\x1BNHi" => 'ï'// 239 in CP1252
  242.             "\x1BNs"  => 'ð'// 240 in CP1252
  243.             "\x1BNDn" => 'ñ'// 241 in CP1252
  244.             "\x1BNAo" => 'ò'// 242 in CP1252
  245.             "\x1BNBo" => 'ó'// 243 in CP1252
  246.             "\x1BNCo" => 'ô'// 244 in CP1252
  247.             "\x1BNDo" => 'õ'// 245 in CP1252
  248.             "\x1BNHo" => 'ö'// 246 in CP1252
  249.             "\x1B/7"  => '÷'// 247 in CP1252
  250.             "\x1BNy"  => 'ø'// 248 in CP1252
  251.             "\x1BNAu" => 'ù'// 249 in CP1252
  252.             "\x1BNBu" => 'ú'// 250 in CP1252
  253.             "\x1BNCu" => 'û'// 251 in CP1252
  254.             "\x1BNHu" => 'ü'// 252 in CP1252
  255.             "\x1B/="  => 'ý'// 253 in CP1252
  256.             "\x1BN|"  => 'þ'// 254 in CP1252
  257.             "\x1BNHy" => 'ÿ'// 255 in CP1252
  258.         );
  259.     }
  260.  
  261.     /**
  262.      * Get whether mbstring extension is available
  263.      *
  264.      * @return boolean 
  265.      */
  266.     public static function getIsMbstringEnabled()
  267.     {
  268.         if (isset(self::$_isMbstringEnabled)) {
  269.             return self::$_isMbstringEnabled;
  270.         }
  271.  
  272.         self::$_isMbstringEnabled function_exists('mb_convert_encoding'?
  273.             true false;
  274.  
  275.         return self::$_isMbstringEnabled;
  276.     }
  277.  
  278.     /**
  279.      * Get whether iconv extension is available
  280.      *
  281.      * @return boolean 
  282.      */
  283.     public static function getIsIconvEnabled()
  284.     {
  285.         if (isset(self::$_isIconvEnabled)) {
  286.             return self::$_isIconvEnabled;
  287.         }
  288.  
  289.         // Fail if iconv doesn't exist
  290.         if (!function_exists('iconv')) {
  291.             self::$_isIconvEnabled false;
  292.             return false;
  293.         }
  294.  
  295.         // Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false,
  296.         if (!@iconv('UTF-8''UTF-16LE''x')) {
  297.             self::$_isIconvEnabled false;
  298.             return false;
  299.         }
  300.  
  301.         // Sometimes iconv_substr('A', 0, 1, 'UTF-8') just returns false in PHP 5.2.0
  302.         // we cannot use iconv in that case either (http://bugs.php.net/bug.php?id=37773)
  303.         if (!@iconv('UTF-8''UTF-16LE''x')) {
  304.             self::$_isIconvEnabled false;
  305.             return false;
  306.         }
  307.  
  308.         // CUSTOM: IBM AIX iconv() does not work
  309.         if defined('PHP_OS'&& @stristr(PHP_OS'AIX')
  310.                 && defined('ICONV_IMPL'&& (@strcasecmp(ICONV_IMPL'unknown'== 0)
  311.                 && defined('ICONV_VERSION'&& (@strcasecmp(ICONV_VERSION'unknown'== 0) )
  312.         {
  313.             self::$_isIconvEnabled false;
  314.             return false;
  315.         }
  316.  
  317.         // If we reach here no problems were detected with iconv
  318.         self::$_isIconvEnabled true;
  319.         return true;
  320.     }
  321.  
  322.     /**
  323.      * Convert from OpenXML escaped control character to PHP control character
  324.      *
  325.      * Excel 2007 team:
  326.      * ----------------
  327.      * That's correct, control characters are stored directly in the shared-strings table.
  328.      * We do encode characters that cannot be represented in XML using the following escape sequence:
  329.      * _xHHHH_ where H represents a hexadecimal character in the character's value...
  330.      * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  331.      * element or in the shared string <t> element.
  332.      *
  333.      * @param     string    $value    Value to unescape
  334.      * @return     string 
  335.      */
  336.     public static function ControlCharacterOOXML2PHP($value ''{
  337.         if(empty(self::$_controlCharacters)) {
  338.             self::_buildControlCharacters();
  339.         }
  340.  
  341.         return str_replacearray_keys(self::$_controlCharacters)array_values(self::$_controlCharacters)$value );
  342.     }
  343.  
  344.     /**
  345.      * Convert from PHP control character to OpenXML escaped control character
  346.      *
  347.      * Excel 2007 team:
  348.      * ----------------
  349.      * That's correct, control characters are stored directly in the shared-strings table.
  350.      * We do encode characters that cannot be represented in XML using the following escape sequence:
  351.      * _xHHHH_ where H represents a hexadecimal character in the character's value...
  352.      * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  353.      * element or in the shared string <t> element.
  354.      *
  355.      * @param     string    $value    Value to escape
  356.      * @return     string 
  357.      */
  358.     public static function ControlCharacterPHP2OOXML($value ''{
  359.         if(empty(self::$_controlCharacters)) {
  360.             self::_buildControlCharacters();
  361.         }
  362.  
  363.         return str_replacearray_values(self::$_controlCharacters)array_keys(self::$_controlCharacters)$value );
  364.     }
  365.  
  366.     /**
  367.      * Try to sanitize UTF8, stripping invalid byte sequences. Not perfect. Does not surrogate characters.
  368.      *
  369.      * @param string $value 
  370.      * @return string 
  371.      */
  372.     public static function SanitizeUTF8($value)
  373.     {
  374.         if (self::getIsIconvEnabled()) {
  375.             $value @iconv('UTF-8''UTF-8'$value);
  376.             return $value;
  377.         }
  378.  
  379.         if (self::getIsMbstringEnabled()) {
  380.             $value mb_convert_encoding($value'UTF-8''UTF-8');
  381.             return $value;
  382.         }
  383.  
  384.         // else, no conversion
  385.         return $value;
  386.     }
  387.  
  388.     /**
  389.      * Check if a string contains UTF8 data
  390.      *
  391.      * @param string $value 
  392.      * @return boolean 
  393.      */
  394.     public static function IsUTF8($value ''{
  395.         return utf8_encode(utf8_decode($value)) === $value;
  396.     }
  397.  
  398.     /**
  399.      * Formats a numeric value as a string for output in various output writers forcing
  400.      * point as decimal separator in case locale is other than English.
  401.      *
  402.      * @param mixed $value 
  403.      * @return string 
  404.      */
  405.     public static function FormatNumber($value{
  406.         if (is_float($value)) {
  407.             return str_replace(',''.'$value);
  408.         }
  409.         return (string) $value;
  410.     }
  411.  
  412.     /**
  413.      * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length)
  414.      * Writes the string using uncompressed notation, no rich text, no Asian phonetics
  415.      * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
  416.      * although this will give wrong results for non-ASCII strings
  417.      * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
  418.      *
  419.      * @param string $value UTF-8 encoded string
  420.      * @return string 
  421.      */
  422.     public static function UTF8toBIFF8UnicodeShort($value)
  423.     {
  424.         // character count
  425.         $ln self::CountCharacters($value'UTF-8');
  426.  
  427.         // option flags
  428.         $opt (self::getIsIconvEnabled(|| self::getIsMbstringEnabled()) ?
  429.             0x0001 0x0000;
  430.  
  431.         // characters
  432.         $chars self::ConvertEncoding($value'UTF-16LE''UTF-8');
  433.  
  434.         $data pack('CC'$ln$opt$chars;
  435.         return $data;
  436.     }
  437.  
  438.     /**
  439.      * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length)
  440.      * Writes the string using uncompressed notation, no rich text, no Asian phonetics
  441.      * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
  442.      * although this will give wrong results for non-ASCII strings
  443.      * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
  444.      *
  445.      * @param string $value UTF-8 encoded string
  446.      * @return string 
  447.      */
  448.     public static function UTF8toBIFF8UnicodeLong($value)
  449.     {
  450.         // character count
  451.         $ln self::CountCharacters($value'UTF-8');
  452.  
  453.         // option flags
  454.         $opt (self::getIsIconvEnabled(|| self::getIsMbstringEnabled()) ?
  455.             0x0001 0x0000;
  456.  
  457.         // characters
  458.         $chars self::ConvertEncoding($value'UTF-16LE''UTF-8');
  459.  
  460.         $data pack('vC'$ln$opt$chars;
  461.         return $data;
  462.     }
  463.  
  464.     /**
  465.      * Convert string from one encoding to another. First try iconv, then mbstring, or no convertion
  466.      *
  467.      * @param string $value 
  468.      * @param string $to Encoding to convert to, e.g. 'UTF-8'
  469.      * @param string $from Encoding to convert from, e.g. 'UTF-16LE'
  470.      * @return string 
  471.      */
  472.     public static function ConvertEncoding($value$to$from)
  473.     {
  474.         if (self::getIsIconvEnabled()) {
  475.             $value iconv($from$to$value);
  476.             return $value;
  477.         }
  478.  
  479.         if (self::getIsMbstringEnabled()) {
  480.             $value mb_convert_encoding($value$to$from);
  481.             return $value;
  482.         }
  483.         if($from == 'UTF-16LE'){
  484.             return self::utf16_decode($valuefalse);
  485.         }else if($from == 'UTF-16BE'){
  486.             return self::utf16_decode($value);
  487.         }
  488.         // else, no conversion
  489.         return $value;
  490.     }
  491.     
  492.     /**
  493.      * Decode UTF-16 encoded strings.
  494.      *
  495.      * Can handle both BOM'ed data and un-BOM'ed data.
  496.      * Assumes Big-Endian byte order if no BOM is available.
  497.      * This function was taken from http://php.net/manual/en/function.utf8-decode.php
  498.      * and $bom_be parameter added.
  499.      *
  500.      * @param   string  $str  UTF-16 encoded data to decode.
  501.      * @return  string  UTF-8 / ISO encoded data.
  502.      * @access  public
  503.      * @version 0.2 / 2010-05-13
  504.      * @author  Rasmus Andersson {@link http://rasmusandersson.se/}
  505.      * @author vadik56
  506.      */
  507.     function utf16_decode$str$bom_be=true {
  508.         ifstrlen($strreturn $str;
  509.         $c0 ord($str{0});
  510.         $c1 ord($str{1});
  511.         if$c0 == 0xfe && $c1 == 0xff $str substr($str,2)}
  512.         elseif$c0 == 0xff && $c1 == 0xfe $str substr($str,2)$bom_be false}
  513.         $len strlen($str);
  514.         $newstr '';
  515.         for($i=0;$i<$len;$i+=2{
  516.             if$bom_be $val ord($str{$i})   << 4$val += ord($str{$i+1})}
  517.             else {        $val ord($str{$i+1}<< 4$val += ord($str{$i})}
  518.             $newstr .= ($val == 0x228"\n" chr($val);
  519.         }
  520.         return $newstr;
  521.     }
  522.  
  523.     /**
  524.      * Get character count. First try mbstring, then iconv, finally strlen
  525.      *
  526.      * @param string $value 
  527.      * @param string $enc Encoding
  528.      * @return int Character count
  529.      */
  530.     public static function CountCharacters($value$enc 'UTF-8')
  531.     {
  532.         if (self::getIsIconvEnabled()) {
  533.             $count iconv_strlen($value$enc);
  534.             return $count;
  535.         }
  536.  
  537.         if (self::getIsMbstringEnabled()) {
  538.             $count mb_strlen($value$enc);
  539.             return $count;
  540.         }
  541.  
  542.         // else strlen
  543.         $count strlen($value);
  544.         return $count;
  545.     }
  546.  
  547.     /**
  548.      * Get a substring of a UTF-8 encoded string
  549.      *
  550.      * @param string $pValue UTF-8 encoded string
  551.      * @param int $start Start offset
  552.      * @param int $length Maximum number of characters in substring
  553.      * @return string 
  554.      */
  555.     public static function Substring($pValue ''$pStart 0$pLength 0)
  556.     {
  557.         if (self::getIsIconvEnabled()) {
  558.             $string iconv_substr($pValue$pStart$pLength'UTF-8');
  559.             return $string;
  560.         }
  561.  
  562.         if (self::getIsMbstringEnabled()) {
  563.             $string mb_substr($pValue$pStart$pLength'UTF-8');
  564.             return $string;
  565.         }
  566.  
  567.         // else substr
  568.         $string substr($pValue$pStart$pLength);
  569.         return $string;
  570.     }
  571.  
  572.  
  573.     /**
  574.      * Identify whether a string contains a fractional numeric value,
  575.      *    and convert it to a numeric if it is
  576.      *
  577.      * @param string &$operand string value to test
  578.      * @return boolean 
  579.      */
  580.     public static function convertToNumberIfFraction(&$operand{
  581.         if (preg_match('/^'.self::STRING_REGEXP_FRACTION.'$/i'$operand$match)) {
  582.             $sign ($match[1== '-''-' '+';
  583.             $fractionFormula '='.$sign.$match[2].$sign.$match[3];
  584.             $operand PHPExcel_Calculation::getInstance()->_calculateFormulaValue($fractionFormula);
  585.             return true;
  586.         }
  587.         return false;
  588.     }    //    function convertToNumberIfFraction()
  589.  
  590.     /**
  591.      * Get the decimal separator. If it has not yet been set explicitly, try to obtain number
  592.      * formatting information from locale.
  593.      *
  594.      * @return string 
  595.      */
  596.     public static function getDecimalSeparator()
  597.     {
  598.         if (!isset(self::$_decimalSeparator)) {
  599.             $localeconv localeconv();
  600.             self::$_decimalSeparator $localeconv['decimal_point'!= ''
  601.                 ? $localeconv['decimal_point'$localeconv['mon_decimal_point'];
  602.                 
  603.             if (self::$_decimalSeparator == '')
  604.             {
  605.                 // Default to .
  606.                 self::$_decimalSeparator '.';
  607.             }
  608.         }
  609.         return self::$_decimalSeparator;
  610.     }
  611.  
  612.     /**
  613.      * Set the decimal separator. Only used by PHPExcel_Style_NumberFormat::toFormattedString()
  614.      * to format output by PHPExcel_Writer_HTML and PHPExcel_Writer_PDF
  615.      *
  616.      * @param string $pValue Character for decimal separator
  617.      */
  618.     public static function setDecimalSeparator($pValue '.')
  619.     {
  620.         self::$_decimalSeparator $pValue;
  621.     }
  622.  
  623.     /**
  624.      * Get the thousands separator. If it has not yet been set explicitly, try to obtain number
  625.      * formatting information from locale.
  626.      *
  627.      * @return string 
  628.      */
  629.     public static function getThousandsSeparator()
  630.     {
  631.         if (!isset(self::$_thousandsSeparator)) {
  632.             $localeconv localeconv();
  633.             self::$_thousandsSeparator $localeconv['thousands_sep'!= ''
  634.                 ? $localeconv['thousands_sep'$localeconv['mon_thousands_sep'];
  635.         }
  636.         return self::$_thousandsSeparator;
  637.     }
  638.  
  639.     /**
  640.      * Set the thousands separator. Only used by PHPExcel_Style_NumberFormat::toFormattedString()
  641.      * to format output by PHPExcel_Writer_HTML and PHPExcel_Writer_PDF
  642.      *
  643.      * @param string $pValue Character for thousands separator
  644.      */
  645.     public static function setThousandsSeparator($pValue ',')
  646.     {
  647.         self::$_thousandsSeparator $pValue;
  648.     }
  649.  
  650.     /**
  651.      * Convert SYLK encoded string to UTF-8
  652.      *
  653.      * @param string $pValue 
  654.      * @return string UTF-8 encoded string
  655.      */
  656.     public static function SYLKtoUTF8($pValue '')
  657.     {
  658.         // If there is no escape character in the string there is nothing to do
  659.         if (strpos($pValue''=== false{
  660.             return $pValue;
  661.         }
  662.  
  663.         if(empty(self::$_SYLKCharacters)) {
  664.             self::_buildSYLKCharacters();
  665.         }
  666.  
  667.         foreach (self::$_SYLKCharacters as $k => $v{
  668.             $pValue str_replace($k$v$pValue);
  669.         }
  670.  
  671.         return $pValue;
  672.     }
  673.  
  674. }

Documentation generated on Tue, 01 Jun 2010 17:06:11 +0200 by phpDocumentor 1.4.3