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.2, 2010-01-11
  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.      * Decimal separator
  50.      *
  51.      * @var string 
  52.      */
  53.     private static $_decimalSeparator;
  54.  
  55.     /**
  56.      * Thousands separator
  57.      *
  58.      * @var string 
  59.      */
  60.     private static $_thousandsSeparator;
  61.  
  62.     /**
  63.      * Is mbstring extension avalable?
  64.      *
  65.      * @var boolean 
  66.      */
  67.     private static $_isMbstringEnabled;
  68.  
  69.     /**
  70.      * Is iconv extension avalable?
  71.      *
  72.      * @var boolean 
  73.      */
  74.     private static $_isIconvEnabled;
  75.  
  76.     /**
  77.      * Build control characters array
  78.      */
  79.     private static function _buildControlCharacters({
  80.         for ($i 0$i <= 31++$i{
  81.             if ($i != && $i != 10 && $i != 13{
  82.                 $find '_x' sprintf('%04s' strtoupper(dechex($i))) '_';
  83.                 $replace chr($i);
  84.                 self::$_controlCharacters[$find$replace;
  85.             }
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Get whether mbstring extension is available
  91.      *
  92.      * @return boolean 
  93.      */
  94.     public static function getIsMbstringEnabled()
  95.     {
  96.         if (isset(self::$_isMbstringEnabled)) {
  97.             return self::$_isMbstringEnabled;
  98.         }
  99.  
  100.         self::$_isMbstringEnabled function_exists('mb_convert_encoding'?
  101.             true false;
  102.  
  103.         return self::$_isMbstringEnabled;
  104.     }
  105.  
  106.     /**
  107.      * Get whether iconv extension is available
  108.      *
  109.      * @return boolean 
  110.      */
  111.     public static function getIsIconvEnabled()
  112.     {
  113.         if (isset(self::$_isIconvEnabled)) {
  114.             return self::$_isIconvEnabled;
  115.         }
  116.  
  117.         // Check that iconv exists
  118.         // Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false,
  119.         // we cannot use iconv when that happens
  120.         // Also, sometimes iconv_substr('A', 0, 1, 'UTF-8') just returns false in PHP 5.2.0
  121.         // we cannot use iconv in that case either (http://bugs.php.net/bug.php?id=37773)
  122.         if (function_exists('iconv')
  123.             && @iconv('UTF-8''UTF-16LE''x')
  124.             && @iconv_substr('A'01'UTF-8') ) {
  125.  
  126.             self::$_isIconvEnabled true;
  127.         else {
  128.             self::$_isIconvEnabled false;
  129.         }
  130.  
  131.         return self::$_isIconvEnabled;
  132.     }
  133.  
  134.     /**
  135.      * Convert from OpenXML escaped control character to PHP control character
  136.      *
  137.      * Excel 2007 team:
  138.      * ----------------
  139.      * That's correct, control characters are stored directly in the shared-strings table.
  140.      * We do encode characters that cannot be represented in XML using the following escape sequence:
  141.      * _xHHHH_ where H represents a hexadecimal character in the character's value...
  142.      * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  143.      * element or in the shared string <t> element.
  144.      *
  145.      * @param     string    $value    Value to unescape
  146.      * @return     string 
  147.      */
  148.     public static function ControlCharacterOOXML2PHP($value ''{
  149.         if(empty(self::$_controlCharacters)) {
  150.             self::_buildControlCharacters();
  151.         }
  152.  
  153.         return str_replacearray_keys(self::$_controlCharacters)array_values(self::$_controlCharacters)$value );
  154.     }
  155.  
  156.     /**
  157.      * Convert from PHP control character to OpenXML escaped control character
  158.      *
  159.      * Excel 2007 team:
  160.      * ----------------
  161.      * That's correct, control characters are stored directly in the shared-strings table.
  162.      * We do encode characters that cannot be represented in XML using the following escape sequence:
  163.      * _xHHHH_ where H represents a hexadecimal character in the character's value...
  164.      * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  165.      * element or in the shared string <t> element.
  166.      *
  167.      * @param     string    $value    Value to escape
  168.      * @return     string 
  169.      */
  170.     public static function ControlCharacterPHP2OOXML($value ''{
  171.         if(empty(self::$_controlCharacters)) {
  172.             self::_buildControlCharacters();
  173.         }
  174.  
  175.         return str_replacearray_values(self::$_controlCharacters)array_keys(self::$_controlCharacters)$value );
  176.     }
  177.  
  178.     /**
  179.      * Try to sanitize UTF8, stripping invalid byte sequences. Not perfect. Does not surrogate characters.
  180.      *
  181.      * @param string $value 
  182.      * @return string 
  183.      */
  184.     public static function SanitizeUTF8($value)
  185.     {
  186.         if (self::getIsIconvEnabled()) {
  187.             $value @iconv('UTF-8''UTF-8'$value);
  188.             return $value;
  189.         }
  190.  
  191.         if (self::getIsMbstringEnabled()) {
  192.             $value mb_convert_encoding($value'UTF-8''UTF-8');
  193.             return $value;
  194.         }
  195.  
  196.         // else, no conversion
  197.         return $value;
  198.     }
  199.  
  200.     /**
  201.      * Check if a string contains UTF8 data
  202.      *
  203.      * @param string $value 
  204.      * @return boolean 
  205.      */
  206.     public static function IsUTF8($value ''{
  207.         return utf8_encode(utf8_decode($value)) === $value;
  208.     }
  209.  
  210.     /**
  211.      * Formats a numeric value as a string for output in various output writers forcing
  212.      * point as decimal separator in case locale is other than English.
  213.      *
  214.      * @param mixed $value 
  215.      * @return string 
  216.      */
  217.     public static function FormatNumber($value{
  218.         if (is_float($value)) {
  219.             return str_replace(',''.'$value);
  220.         }
  221.         return (string) $value;
  222.     }
  223.  
  224.     /**
  225.      * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length)
  226.      * Writes the string using uncompressed notation, no rich text, no Asian phonetics
  227.      * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
  228.      * although this will give wrong results for non-ASCII strings
  229.      * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
  230.      *
  231.      * @param string $value UTF-8 encoded string
  232.      * @return string 
  233.      */
  234.     public static function UTF8toBIFF8UnicodeShort($value)
  235.     {
  236.         // character count
  237.         $ln self::CountCharacters($value'UTF-8');
  238.  
  239.         // option flags
  240.         $opt (self::getIsIconvEnabled(|| self::getIsMbstringEnabled()) ?
  241.             0x0001 0x0000;
  242.  
  243.         // characters
  244.         $chars self::ConvertEncoding($value'UTF-16LE''UTF-8');
  245.  
  246.         $data pack('CC'$ln$opt$chars;
  247.         return $data;
  248.     }
  249.  
  250.     /**
  251.      * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length)
  252.      * Writes the string using uncompressed notation, no rich text, no Asian phonetics
  253.      * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
  254.      * although this will give wrong results for non-ASCII strings
  255.      * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
  256.      *
  257.      * @param string $value UTF-8 encoded string
  258.      * @return string 
  259.      */
  260.     public static function UTF8toBIFF8UnicodeLong($value)
  261.     {
  262.         // character count
  263.         $ln self::CountCharacters($value'UTF-8');
  264.  
  265.         // option flags
  266.         $opt (self::getIsIconvEnabled(|| self::getIsMbstringEnabled()) ?
  267.             0x0001 0x0000;
  268.  
  269.         // characters
  270.         $chars self::ConvertEncoding($value'UTF-16LE''UTF-8');
  271.  
  272.         $data pack('vC'$ln$opt$chars;
  273.         return $data;
  274.     }
  275.  
  276.     /**
  277.      * Convert string from one encoding to another. First try mbstring, then iconv, or no convertion
  278.      *
  279.      * @param string $value 
  280.      * @param string $to Encoding to convert to, e.g. 'UTF-8'
  281.      * @param string $from Encoding to convert from, e.g. 'UTF-16LE'
  282.      * @return string 
  283.      */
  284.     public static function ConvertEncoding($value$to$from)
  285.     {
  286.         if (self::getIsIconvEnabled()) {
  287.             $value iconv($from$to$value);
  288.             return $value;
  289.         }
  290.  
  291.         if (self::getIsMbstringEnabled()) {
  292.             $value mb_convert_encoding($value$to$from);
  293.             return $value;
  294.         }
  295.  
  296.         // else, no conversion
  297.         return $value;
  298.     }
  299.  
  300.     /**
  301.      * Get character count. First try mbstring, then iconv, finally strlen
  302.      *
  303.      * @param string $value 
  304.      * @param string $enc Encoding
  305.      * @return int Character count
  306.      */
  307.     public static function CountCharacters($value$enc 'UTF-8')
  308.     {
  309.         if (self::getIsIconvEnabled()) {
  310.             $count iconv_strlen($value$enc);
  311.             return $count;
  312.         }
  313.  
  314.         if (self::getIsMbstringEnabled()) {
  315.             $count mb_strlen($value$enc);
  316.             return $count;
  317.         }
  318.  
  319.         // else strlen
  320.         $count strlen($value);
  321.         return $count;
  322.     }
  323.  
  324.     /**
  325.      * Get a substring of a UTF-8 encoded string
  326.      *
  327.      * @param string $pValue UTF-8 encoded string
  328.      * @param int $start Start offset
  329.      * @param int $length Maximum number of characters in substring
  330.      * @return string 
  331.      */
  332.     public static function Substring($pValue ''$pStart 0$pLength 0)
  333.     {
  334.         if (self::getIsIconvEnabled()) {
  335.             $string iconv_substr($pValue$pStart$pLength'UTF-8');
  336.             return $string;
  337.         }
  338.  
  339.         if (self::getIsMbstringEnabled()) {
  340.             $string mb_substr($pValue$pStart$pLength'UTF-8');
  341.             return $string;
  342.         }
  343.  
  344.         // else substr
  345.         $string substr($pValue$pStart$pLength);
  346.         return $string;
  347.     }
  348.  
  349.  
  350.     /**
  351.      * Identify whether a string contains a fractional numeric value,
  352.      *    and convert it to a numeric if it is
  353.      *
  354.      * @param string &$operand string value to test
  355.      * @return boolean 
  356.      */
  357.     public static function convertToNumberIfFraction(&$operand{
  358.         if (preg_match('/^'.self::STRING_REGEXP_FRACTION.'$/i'$operand$match)) {
  359.             $sign ($match[1== '-''-' '+';
  360.             $fractionFormula '='.$sign.$match[2].$sign.$match[3];
  361.             $operand PHPExcel_Calculation::getInstance()->_calculateFormulaValue($fractionFormula);
  362.             return true;
  363.         }
  364.         return false;
  365.     }    //    function convertToNumberIfFraction()
  366.  
  367.     /**
  368.      * Get the decimal separator. If it has not yet been set explicitly, try to obtain number
  369.      * formatting information from locale.
  370.      *
  371.      * @return string 
  372.      */
  373.     public static function getDecimalSeparator()
  374.     {
  375.         if (!isset(self::$_decimalSeparator)) {
  376.             $localeconv localeconv();
  377.             self::$_decimalSeparator $localeconv['decimal_point'!= ''
  378.                 ? $localeconv['decimal_point'$localeconv['mon_decimal_point'];
  379.                 
  380.             if (self::$_decimalSeparator == '')
  381.             {
  382.                 // Default to .
  383.                 self::$_decimalSeparator '.';
  384.             }
  385.         }
  386.         return self::$_decimalSeparator;
  387.     }
  388.  
  389.     /**
  390.      * Set the decimal separator. Only used by PHPExcel_Style_NumberFormat::toFormattedString()
  391.      * to format output by PHPExcel_Writer_HTML and PHPExcel_Writer_PDF
  392.      *
  393.      * @param string $pValue Character for decimal separator
  394.      */
  395.     public static function setDecimalSeparator($pValue '.')
  396.     {
  397.         self::$_decimalSeparator $pValue;
  398.     }
  399.  
  400.     /**
  401.      * Get the thousands separator. If it has not yet been set explicitly, try to obtain number
  402.      * formatting information from locale.
  403.      *
  404.      * @return string 
  405.      */
  406.     public static function getThousandsSeparator()
  407.     {
  408.         if (!isset(self::$_thousandsSeparator)) {
  409.             $localeconv localeconv();
  410.             self::$_thousandsSeparator $localeconv['thousands_sep'!= ''
  411.                 ? $localeconv['thousands_sep'$localeconv['mon_thousands_sep'];
  412.         }
  413.         return self::$_thousandsSeparator;
  414.     }
  415.  
  416.     /**
  417.      * Set the thousands separator. Only used by PHPExcel_Style_NumberFormat::toFormattedString()
  418.      * to format output by PHPExcel_Writer_HTML and PHPExcel_Writer_PDF
  419.      *
  420.      * @param string $pValue Character for thousands separator
  421.      */
  422.     public static function setThousandsSeparator($pValue ',')
  423.     {
  424.         self::$_thousandsSeparator $pValue;
  425.     }
  426.  
  427. }

Documentation generated on Mon, 11 Jan 2010 08:14:11 +0100 by phpDocumentor 1.4.1