2012-05-06 18:25:40 +02:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-03-26 16:33:20 +07:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
namespace PhpOffice\PhpWord\Shared;
|
|
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Common string functions
|
|
|
|
|
*/
|
2014-03-22 10:06:08 +04:00
|
|
|
class String
|
2012-05-06 18:25:40 +02:00
|
|
|
{
|
2013-12-16 06:40:30 -05:00
|
|
|
/**
|
|
|
|
|
* Control characters array
|
|
|
|
|
*
|
|
|
|
|
* @var string[]
|
|
|
|
|
*/
|
2014-04-04 00:29:57 +07:00
|
|
|
private static $controlCharacters = array();
|
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
|
|
|
|
|
*
|
2014-04-04 00:29:57 +07:00
|
|
|
* @param string $value Value to unescape
|
|
|
|
|
* @return string
|
2013-12-16 06:40:30 -05:00
|
|
|
*/
|
2014-03-23 17:37:26 +07:00
|
|
|
public static function controlCharacterOOXML2PHP($value = '')
|
2013-12-16 06:40:30 -05:00
|
|
|
{
|
2014-04-04 00:29:57 +07:00
|
|
|
if (empty(self::$controlCharacters)) {
|
|
|
|
|
self::buildControlCharacters();
|
2013-12-16 06:40:30 -05:00
|
|
|
}
|
2012-05-06 18:25:40 +02:00
|
|
|
|
2014-04-04 00:29:57 +07:00
|
|
|
return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $value);
|
2013-12-16 06:40:30 -05:00
|
|
|
}
|
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
|
|
|
|
|
*
|
2014-04-04 00:29:57 +07:00
|
|
|
* @param string $value Value to escape
|
|
|
|
|
* @return string
|
2013-12-16 06:40:30 -05:00
|
|
|
*/
|
2014-03-23 17:37:26 +07:00
|
|
|
public static function controlCharacterPHP2OOXML($value = '')
|
2013-12-16 06:40:30 -05:00
|
|
|
{
|
2014-04-04 00:29:57 +07:00
|
|
|
if (empty(self::$controlCharacters)) {
|
|
|
|
|
self::buildControlCharacters();
|
2013-12-16 06:40:30 -05:00
|
|
|
}
|
2012-05-06 18:25:40 +02:00
|
|
|
|
2014-04-04 00:29:57 +07:00
|
|
|
return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $value);
|
2013-12-16 06:40:30 -05:00
|
|
|
}
|
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
|
|
|
|
|
*/
|
2014-03-23 17:37:26 +07:00
|
|
|
public static function isUTF8($value = '')
|
2013-12-16 06:40:30 -05:00
|
|
|
{
|
|
|
|
|
return $value === '' || preg_match('/^./su', $value) === 1;
|
|
|
|
|
}
|
2014-04-01 15:01:30 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return UTF8 encoded value
|
|
|
|
|
*
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function toUTF8($value = '')
|
|
|
|
|
{
|
|
|
|
|
if (!is_null($value) && !self::isUTF8($value)) {
|
|
|
|
|
$value = utf8_encode($value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
2014-04-04 00:29:57 +07:00
|
|
|
|
2014-04-11 21:16:07 +07:00
|
|
|
/**
|
|
|
|
|
* Return name without underscore for < 0.10.0 variable name compatibility
|
|
|
|
|
*
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function removeUnderscorePrefix($value)
|
|
|
|
|
{
|
|
|
|
|
if (!is_null($value)) {
|
|
|
|
|
if (substr($value, 0, 1) == '_') {
|
|
|
|
|
$value = substr($value, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-04 00:29:57 +07:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
}
|