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.6.5, 2009-01-05
  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.      * Build control characters array
  46.      */
  47.     private static function _buildControlCharacters({
  48.         for ($i 0$i <= 19++$i{
  49.             if ($i != && $i != 10 && $i != 13{
  50.                 $find '_x' sprintf('%04s' strtoupper(dechex($i))) '_';
  51.                 $replace chr($i);
  52.                 self::$_controlCharacters[$find$replace;
  53.             }
  54.         }
  55.     }
  56.  
  57.     /**
  58.      * Convert from OpenXML escaped control character to PHP control character
  59.      *
  60.      * Excel 2007 team:
  61.      * ----------------
  62.      * That's correct, control characters are stored directly in the shared-strings table.
  63.      * We do encode characters that cannot be represented in XML using the following escape sequence:
  64.      * _xHHHH_ where H represents a hexadecimal character in the character's value...
  65.      * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  66.      * element or in the shared string <t> element.
  67.      *
  68.      * @param     string    $value    Value to unescape
  69.      * @return     string 
  70.      */
  71.     public static function ControlCharacterOOXML2PHP($value ''{
  72.         if(empty(self::$_controlCharacters)) {
  73.             self::_buildControlCharacters();
  74.         }
  75.  
  76.         return str_replacearray_keys(self::$_controlCharacters)array_values(self::$_controlCharacters)$value );
  77.     }
  78.  
  79.     /**
  80.      * Convert from PHP control character to OpenXML escaped control character
  81.      *
  82.      * Excel 2007 team:
  83.      * ----------------
  84.      * That's correct, control characters are stored directly in the shared-strings table.
  85.      * We do encode characters that cannot be represented in XML using the following escape sequence:
  86.      * _xHHHH_ where H represents a hexadecimal character in the character's value...
  87.      * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  88.      * element or in the shared string <t> element.
  89.      *
  90.      * @param     string    $value    Value to escape
  91.      * @return     string 
  92.      */
  93.     public static function ControlCharacterPHP2OOXML($value ''{
  94.         if(empty(self::$_controlCharacters)) {
  95.             self::_buildControlCharacters();
  96.         }
  97.  
  98.         return str_replacearray_values(self::$_controlCharacters)array_keys(self::$_controlCharacters)$value );
  99.     }
  100.  
  101.     /**
  102.      * Check if a string contains UTF8 data
  103.      *
  104.      * @param string $value 
  105.      * @return boolean 
  106.      */
  107.     public static function IsUTF8($value ''{
  108.         return utf8_encode(utf8_decode($value)) === $value;
  109.     }
  110.  
  111.     /**
  112.      * Formats a numeric value as a string for output in various output writers
  113.      *
  114.      * @param mixed $value 
  115.      * @return string 
  116.      */
  117.     public static function FormatNumber($value{
  118.         return number_format($value2'.''');
  119.     }
  120.  
  121.     /**
  122.      * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length)
  123.      * Writes the string using uncompressed notation, no rich text, no Asian phonetics
  124.      * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
  125.      * although this will give wrong results for non-ASCII strings
  126.      * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
  127.      *
  128.      * @param string $value UTF-8 encoded string
  129.      * @return string 
  130.      */
  131.     public static function UTF8toBIFF8UnicodeShort($value)
  132.     {
  133.         if (function_exists('mb_strlen'and function_exists('mb_convert_encoding')) {
  134.             // character count
  135.             $ln mb_strlen($value'UTF-8');
  136.  
  137.             // option flags
  138.             $opt 0x0001;
  139.  
  140.             // characters
  141.             $chars mb_convert_encoding($value'UTF-16LE''UTF-8');
  142.         else {
  143.             // character count
  144.             $ln strlen($value);
  145.  
  146.             // option flags
  147.             $opt 0x0000;
  148.  
  149.             // characters
  150.             $chars $value;
  151.         }
  152.  
  153.         $data pack('CC'$ln$opt$chars;
  154.         return $data;
  155.     }
  156.  
  157.     /**
  158.      * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length)
  159.      * Writes the string using uncompressed notation, no rich text, no Asian phonetics
  160.      * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
  161.      * although this will give wrong results for non-ASCII strings
  162.      * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
  163.      *
  164.      * @param string $value UTF-8 encoded string
  165.      * @return string 
  166.      */
  167.     public static function UTF8toBIFF8UnicodeLong($value)
  168.     {
  169.         if (function_exists('mb_strlen'and function_exists('mb_convert_encoding')) {
  170.             // character count
  171.             $ln mb_strlen($value'UTF-8');
  172.  
  173.             // option flags
  174.             $opt 0x0001;
  175.  
  176.             // characters
  177.             $chars mb_convert_encoding($value'UTF-16LE''UTF-8');
  178.         else {
  179.             // character count
  180.             $ln strlen($value);
  181.  
  182.             // option flags
  183.             $opt 0x0000;
  184.  
  185.             // characters
  186.             $chars $value;
  187.         }
  188.  
  189.         $data pack('vC'$ln$opt$chars;
  190.         return $data;
  191.     }
  192.  
  193. }

Documentation generated on Mon, 05 Jan 2009 20:38:34 +0100 by phpDocumentor 1.4.1