PHPWord/src/PhpWord/Shared/String.php

95 lines
2.9 KiB
PHP
Raw Normal View History

2012-05-06 18:25:40 +02:00
<?php
/**
* PHPWord
2012-05-06 18:25:40 +02:00
*
2014-03-27 23:55:06 +07:00
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
2012-05-06 18:25:40 +02:00
*/
namespace PhpOffice\PhpWord\Shared;
/**
* Common string functions
*/
class String
2012-05-06 18:25:40 +02:00
{
2013-12-16 06:40:30 -05:00
/**
* Control characters array
*
* @var string[]
*/
private static $_controlCharacters = array();
/**
* Build control characters array
*/
private static function _buildControlCharacters()
{
for ($i = 0; $i <= 19; ++$i) {
if ($i != 9 && $i != 10 && $i != 13) {
$find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
$replace = chr($i);
self::$_controlCharacters[$find] = $replace;
}
}
}
2012-05-06 18:25:40 +02:00
2013-12-16 06:40:30 -05:00
/**
* Convert from OpenXML escaped control character to PHP control character
*
* Excel 2007 team:
* ----------------
* That's correct, control characters are stored directly in the shared-strings table.
* We do encode characters that cannot be represented in XML using the following escape sequence:
* _xHHHH_ where H represents a hexadecimal character in the character's value...
* So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
* element or in the shared string <t> element.
*
* @param string $value Value to unescape
* @return string
*/
public static function controlCharacterOOXML2PHP($value = '')
2013-12-16 06:40:30 -05:00
{
if (empty(self::$_controlCharacters)) {
self::_buildControlCharacters();
}
2012-05-06 18:25:40 +02:00
2013-12-16 06:40:30 -05:00
return str_replace(array_keys(self::$_controlCharacters), array_values(self::$_controlCharacters), $value);
}
2012-05-06 18:25:40 +02:00
2013-12-16 06:40:30 -05:00
/**
* Convert from PHP control character to OpenXML escaped control character
*
* Excel 2007 team:
* ----------------
* That's correct, control characters are stored directly in the shared-strings table.
* We do encode characters that cannot be represented in XML using the following escape sequence:
* _xHHHH_ where H represents a hexadecimal character in the character's value...
* So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
* element or in the shared string <t> element.
*
* @param string $value Value to escape
* @return string
*/
public static function controlCharacterPHP2OOXML($value = '')
2013-12-16 06:40:30 -05:00
{
if (empty(self::$_controlCharacters)) {
self::_buildControlCharacters();
}
2012-05-06 18:25:40 +02:00
2013-12-16 06:40:30 -05:00
return str_replace(array_values(self::$_controlCharacters), array_keys(self::$_controlCharacters), $value);
}
2012-05-06 18:25:40 +02:00
2013-12-16 06:40:30 -05:00
/**
* Check if a string contains UTF-8 data
*
* @param string $value
* @return boolean
*/
public static function isUTF8($value = '')
2013-12-16 06:40:30 -05:00
{
return $value === '' || preg_match('/^./su', $value) === 1;
}
2012-05-06 18:25:40 +02:00
}