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 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.0, 2009-08-10
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Shared_String
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Shared
  34.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. {
  37.     /**
  38.      * Control characters array
  39.      *
  40.      * @var string[] 
  41.      */
  42.     private static $_controlCharacters array();
  43.  
  44.     /**
  45.      * Is mbstring extension avalable?
  46.      *
  47.      * @var boolean 
  48.      */
  49.     private static $_isMbstringEnabled;
  50.  
  51.     /**
  52.      * Is iconv extension avalable?
  53.      *
  54.      * @var boolean 
  55.      */
  56.     private static $_isIconvEnabled;
  57.  
  58.     /**
  59.      * Build control characters array
  60.      */
  61.     private static function _buildControlCharacters({
  62.         for ($i 0$i <= 19++$i{
  63.             if ($i != && $i != 10 && $i != 13{
  64.                 $find '_x' sprintf('%04s' strtoupper(dechex($i))) '_';
  65.                 $replace chr($i);
  66.                 self::$_controlCharacters[$find$replace;
  67.             }
  68.         }
  69.     }
  70.  
  71.     /**
  72.      * Get whether mbstring extension is available
  73.      *
  74.      * @return boolean 
  75.      */
  76.     public static function getIsMbstringEnabled()
  77.     {
  78.         if (isset(self::$_isMbstringEnabled)) {
  79.             return self::$_isMbstringEnabled;
  80.         }
  81.  
  82.         self::$_isMbstringEnabled function_exists('mb_convert_encoding'?
  83.             true false;
  84.  
  85.         return self::$_isMbstringEnabled;
  86.     }
  87.  
  88.     /**
  89.      * Get whether iconv extension is available
  90.      *
  91.      * @return boolean 
  92.      */
  93.     public static function getIsIconvEnabled()
  94.     {
  95.         if (isset(self::$_isIconvEnabled)) {
  96.             return self::$_isIconvEnabled;
  97.         }
  98.  
  99.         self::$_isIconvEnabled function_exists('iconv'?
  100.             true false;
  101.  
  102.         return self::$_isIconvEnabled;
  103.     }
  104.  
  105.     /**
  106.      * Convert from OpenXML escaped control character to PHP control character
  107.      *
  108.      * Excel 2007 team:
  109.      * ----------------
  110.      * That's correct, control characters are stored directly in the shared-strings table.
  111.      * We do encode characters that cannot be represented in XML using the following escape sequence:
  112.      * _xHHHH_ where H represents a hexadecimal character in the character's value...
  113.      * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  114.      * element or in the shared string <t> element.
  115.      *
  116.      * @param     string    $value    Value to unescape
  117.      * @return     string 
  118.      */
  119.     public static function ControlCharacterOOXML2PHP($value ''{
  120.         if(empty(self::$_controlCharacters)) {
  121.             self::_buildControlCharacters();
  122.         }
  123.  
  124.         return str_replacearray_keys(self::$_controlCharacters)array_values(self::$_controlCharacters)$value );
  125.     }
  126.  
  127.     /**
  128.      * Convert from PHP control character to OpenXML escaped control character
  129.      *
  130.      * Excel 2007 team:
  131.      * ----------------
  132.      * That's correct, control characters are stored directly in the shared-strings table.
  133.      * We do encode characters that cannot be represented in XML using the following escape sequence:
  134.      * _xHHHH_ where H represents a hexadecimal character in the character's value...
  135.      * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  136.      * element or in the shared string <t> element.
  137.      *
  138.      * @param     string    $value    Value to escape
  139.      * @return     string 
  140.      */
  141.     public static function ControlCharacterPHP2OOXML($value ''{
  142.         if(empty(self::$_controlCharacters)) {
  143.             self::_buildControlCharacters();
  144.         }
  145.  
  146.         return str_replacearray_values(self::$_controlCharacters)array_keys(self::$_controlCharacters)$value );
  147.     }
  148.  
  149.     /**
  150.      * Try to sanitize UTF8, stripping invalid byte sequences. Not perfect. Does not surrogate characters.
  151.      *
  152.      * @param string $value 
  153.      * @return string 
  154.      */
  155.     public static function SanitizeUTF8($value)
  156.     {
  157.         if (self::getIsIconvEnabled()) {
  158.             $value @iconv('UTF-8''UTF-8'$value);
  159.             return $value;
  160.         }
  161.  
  162.         if (self::getIsMbstringEnabled()) {
  163.             $value mb_convert_encoding($value'UTF-8''UTF-8');
  164.             return $value;
  165.         }
  166.  
  167.         // else, no conversion
  168.         return $value;
  169.     }
  170.     
  171.     /**
  172.      * Check if a string contains UTF8 data
  173.      *
  174.      * @param string $value 
  175.      * @return boolean 
  176.      */
  177.     public static function IsUTF8($value ''{
  178.         return utf8_encode(utf8_decode($value)) === $value;
  179.     }
  180.  
  181.     /**
  182.      * Formats a numeric value as a string for output in various output writers forcing
  183.      * point as decimal separator in case locale is other than English.
  184.      *
  185.      * @param mixed $value 
  186.      * @return string 
  187.      */
  188.     public static function FormatNumber($value{
  189.         if (is_float($value)) {
  190.             return str_replace(',''.'$value);
  191.         }
  192.         return (string) $value;
  193.     }
  194.  
  195.     /**
  196.      * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length)
  197.      * Writes the string using uncompressed notation, no rich text, no Asian phonetics
  198.      * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
  199.      * although this will give wrong results for non-ASCII strings
  200.      * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
  201.      *
  202.      * @param string $value UTF-8 encoded string
  203.      * @return string 
  204.      */
  205.     public static function UTF8toBIFF8UnicodeShort($value)
  206.     {
  207.         // character count
  208.         $ln self::CountCharacters($value'UTF-8');
  209.  
  210.         // option flags
  211.         $opt (self::getIsIconvEnabled(|| self::getIsMbstringEnabled()) 
  212.             0x0001 0x0000;
  213.  
  214.         // characters
  215.         $chars self::ConvertEncoding($value'UTF-16LE''UTF-8');
  216.  
  217.         $data pack('CC'$ln$opt$chars;
  218.         return $data;
  219.     }
  220.  
  221.     /**
  222.      * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length)
  223.      * Writes the string using uncompressed notation, no rich text, no Asian phonetics
  224.      * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
  225.      * although this will give wrong results for non-ASCII strings
  226.      * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
  227.      *
  228.      * @param string $value UTF-8 encoded string
  229.      * @return string 
  230.      */
  231.     public static function UTF8toBIFF8UnicodeLong($value)
  232.     {
  233.         // character count
  234.         $ln self::CountCharacters($value'UTF-8');
  235.  
  236.         // option flags
  237.         $opt (self::getIsIconvEnabled(|| self::getIsMbstringEnabled()) 
  238.             0x0001 0x0000;
  239.  
  240.         // characters
  241.         $chars self::ConvertEncoding($value'UTF-16LE''UTF-8');
  242.  
  243.         $data pack('vC'$ln$opt$chars;
  244.         return $data;
  245.     }
  246.  
  247.     /**
  248.      * Convert string from one encoding to another. First try mbstring, then iconv, or no convertion
  249.      *
  250.      * @param string $value 
  251.      * @param string $to Encoding to convert to, e.g. 'UTF-8'
  252.      * @param string $from Encoding to convert from, e.g. 'UTF-16LE'
  253.      * @return string 
  254.      */
  255.     public static function ConvertEncoding($value$to$from)
  256.     {
  257.         if (self::getIsIconvEnabled()) {
  258.             $value iconv($from$to$value);
  259.             return $value;
  260.         }
  261.  
  262.         if (self::getIsMbstringEnabled()) {
  263.             $value mb_convert_encoding($value$to$from);
  264.             return $value;
  265.         }
  266.  
  267.         // else, no conversion
  268.         return $value;
  269.     }
  270.     
  271.     /**
  272.      * Get character count. First try mbstring, then iconv, finally strlen
  273.      *
  274.      * @param string $value 
  275.      * @param string $enc Encoding
  276.      * @return int Character count
  277.      */
  278.     public static function CountCharacters($value$enc 'UTF-8')
  279.     {
  280.         if (self::getIsIconvEnabled()) {
  281.             $count iconv_strlen($value$enc);
  282.             return $count;
  283.         }
  284.  
  285.         if (self::getIsMbstringEnabled()) {
  286.             $count mb_strlen($value$enc);
  287.             return $count;
  288.         }
  289.  
  290.         // else strlen
  291.         $count strlen($value);
  292.         return $count;
  293.     }
  294.  
  295.     /**
  296.      * Get a substring of a UTF-8 encoded string
  297.      *
  298.      * @param string $pValue UTF-8 encoded string
  299.      * @param int $start Start offset
  300.      * @param int $length Maximum number of characters in substring
  301.      * @return string 
  302.      */
  303.     public static function Substring($pValue ''$pStart 0$pLength 0)
  304.     {
  305.         if (self::getIsIconvEnabled()) {
  306.             $string iconv_substr($pValue$pStart$pLength'UTF-8');
  307.             return $string;
  308.         }
  309.  
  310.         if (self::getIsMbstringEnabled()) {
  311.             $string mb_substr($pValue$pStart$pLength'UTF-8');
  312.             return $string;
  313.         }
  314.  
  315.         // else substr
  316.         $string substr($pValue$pStart$pLength);
  317.         return $string;
  318.     }
  319.  
  320. }

Documentation generated on Mon, 10 Aug 2009 08:08:03 +0200 by phpDocumentor 1.4.1