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

Documentation generated on Sun, 27 Feb 2011 16:34:15 -0800 by phpDocumentor 1.4.3