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 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.4, 2008-10-27
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Shared_String
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Shared
  34.  * @copyright  Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. {
  37.     /**
  38.      * Convert from OpenXML escaped control character to PHP control character
  39.      *
  40.      * Excel 2007 team:
  41.      * ----------------
  42.      * That's correct, control characters are stored directly in the shared-strings table.
  43.      * We do encode characters that cannot be represented in XML using the following escape sequence:
  44.      * _xHHHH_ where H represents a hexadecimal character in the character's value...
  45.      * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  46.      * element or in the shared string <t> element.
  47.      *
  48.      * @param     string    $value    Value to unescape
  49.      * @return     string 
  50.      */
  51.     public static function ControlCharacterOOXML2PHP($value ''{
  52.         for ($i 0$i <= 19++$i{
  53.             if ($i != && $i != 10 && $i != 13{
  54.                 $value str_replace('_x' sprintf('%04s' strtoupper(dechex($i))) '_'chr($i)$value);
  55.             }
  56.         }
  57.  
  58.         return $value;
  59.     }
  60.  
  61.     /**
  62.      * Convert from PHP control character to OpenXML escaped control character
  63.      *
  64.      * Excel 2007 team:
  65.      * ----------------
  66.      * That's correct, control characters are stored directly in the shared-strings table.
  67.      * We do encode characters that cannot be represented in XML using the following escape sequence:
  68.      * _xHHHH_ where H represents a hexadecimal character in the character's value...
  69.      * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  70.      * element or in the shared string <t> element.
  71.      *
  72.      * @param     string    $value    Value to escape
  73.      * @return     string 
  74.      */
  75.     public static function ControlCharacterPHP2OOXML($value ''{
  76.         for ($i 0$i <= 19++$i{
  77.             if ($i != && $i != 10 && $i != 13{
  78.                 $value str_replace(chr($i)'_x' sprintf('%04s' strtoupper(dechex($i))) '_'$value);
  79.             }
  80.         }
  81.  
  82.         return $value;
  83.     }
  84.  
  85.     /**
  86.      * Check if a string contains UTF8 data
  87.      *
  88.      * @param string $value 
  89.      * @return boolean 
  90.      */
  91.     public static function IsUTF8($value ''{
  92.         return utf8_encode(utf8_decode($value)) === $value;
  93.     }
  94.  
  95.     /**
  96.      * Formats a numeric value as a string for output in various output writers
  97.      *
  98.      * @param mixed $value 
  99.      * @return string 
  100.      */
  101.     public static function FormatNumber($value{
  102.         return number_format($value2'.''');
  103.     }
  104.  
  105.     /**
  106.      * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length)
  107.      * Writes the string using uncompressed notation, no rich text, no Asian phonetics
  108.      * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
  109.      * although this will give wrong results for non-ASCII strings
  110.      * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
  111.      *
  112.      * @param string $value UTF-8 encoded string
  113.      * @return string 
  114.      */
  115.     public static function UTF8toBIFF8UnicodeShort($value)
  116.     {
  117.         if (function_exists('mb_strlen'and function_exists('mb_convert_encoding')) {
  118.             // character count
  119.             $ln mb_strlen($value'UTF-8');
  120.  
  121.             // option flags
  122.             $opt 0x0001;
  123.  
  124.             // characters
  125.             $chars mb_convert_encoding($value'UTF-16LE''UTF-8');
  126.         else {
  127.             // character count
  128.             $ln strlen($value);
  129.  
  130.             // option flags
  131.             $opt 0x0000;
  132.  
  133.             // characters
  134.             $chars $value;
  135.         }
  136.  
  137.         $data pack('CC'$ln$opt$chars;
  138.         return $data;
  139.     }
  140.  
  141.     /**
  142.      * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length)
  143.      * Writes the string using uncompressed notation, no rich text, no Asian phonetics
  144.      * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
  145.      * although this will give wrong results for non-ASCII strings
  146.      * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
  147.      *
  148.      * @param string $value UTF-8 encoded string
  149.      * @return string 
  150.      */
  151.     public static function UTF8toBIFF8UnicodeLong($value)
  152.     {
  153.         if (function_exists('mb_strlen'and function_exists('mb_convert_encoding')) {
  154.             // character count
  155.             $ln mb_strlen($value'UTF-8');
  156.  
  157.             // option flags
  158.             $opt 0x0001;
  159.  
  160.             // characters
  161.             $chars mb_convert_encoding($value'UTF-16LE''UTF-8');
  162.         else {
  163.             // character count
  164.             $ln strlen($value);
  165.  
  166.             // option flags
  167.             $opt 0x0000;
  168.  
  169.             // characters
  170.             $chars $value;
  171.         }
  172.  
  173.         $data pack('vC'$ln$opt$chars;
  174.         return $data;
  175.     }
  176.  
  177. }

Documentation generated on Mon, 27 Oct 2008 08:41:21 +0100 by phpDocumentor 1.4.1