• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PHPWord / 13025401639

29 Jan 2025 05:48AM UTC coverage: 96.825% (-0.4%) from 97.217%
13025401639

Pull #2562

github

web-flow
Merge ebe3e5dac into 2d2759585
Pull Request #2562: TemplateProcessor SetComplexBlock/Value and Sections

6 of 7 new or added lines in 1 file covered. (85.71%)

245 existing lines in 40 files now uncovered.

12227 of 12628 relevant lines covered (96.82%)

34.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.11
/src/PhpWord/Shared/Text.php
1
<?php
2

3
/**
4
 * This file is part of PHPWord - A pure PHP library for reading and writing
5
 * word processing documents.
6
 *
7
 * PHPWord is free software distributed under the terms of the GNU Lesser
8
 * General Public License version 3 as published by the Free Software Foundation.
9
 *
10
 * For the full copyright and license information, please read the LICENSE
11
 * file that was distributed with this source code. For the full list of
12
 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13
 *
14
 * @see         https://github.com/PHPOffice/PHPWord
15
 *
16
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17
 */
18

19
namespace PhpOffice\PhpWord\Shared;
20

21
/**
22
 * Text.
23
 */
24
class Text
25
{
26
    /**
27
     * Control characters array.
28
     *
29
     * @var string[]
30
     */
31
    private static $controlCharacters = [];
32

33
    /**
34
     * Build control characters array.
35
     */
36
    private static function buildControlCharacters(): void
37
    {
38
        for ($i = 0; $i <= 19; ++$i) {
39✔
39
            if ($i != 9 && $i != 10 && $i != 13) {
39✔
40
                $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
39✔
41
                $replace = chr($i);
39✔
42
                self::$controlCharacters[$find] = $replace;
39✔
43
            }
44
        }
45
    }
46

47
    /**
48
     * Convert from PHP control character to OpenXML escaped control character.
49
     *
50
     * Excel 2007 team:
51
     * ----------------
52
     * That's correct, control characters are stored directly in the shared-strings table.
53
     * We do encode characters that cannot be represented in XML using the following escape sequence:
54
     * _xHHHH_ where H represents a hexadecimal character in the character's value...
55
     * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
56
     * element or in the shared string <t> element.
57
     *
58
     * @param  string $value Value to escape
59
     *
60
     * @return string
61
     */
62
    public static function controlCharacterPHP2OOXML($value = '')
63
    {
64
        if (empty(self::$controlCharacters)) {
96✔
65
            self::buildControlCharacters();
39✔
66
        }
67

68
        return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $value);
96✔
69
    }
70

71
    /**
72
     * Return a number formatted for being integrated in xml files.
73
     *
74
     * @param float $number
75
     * @param int $decimals
76
     *
77
     * @return string
78
     */
79
    public static function numberFormat($number, $decimals)
80
    {
81
        return number_format($number, $decimals, '.', '');
1✔
82
    }
83

84
    /**
85
     * @param int $dec
86
     *
87
     * @see http://stackoverflow.com/a/7153133/2235790
88
     *
89
     * @author velcrow
90
     *
91
     * @return string
92
     */
93
    public static function chr($dec)
94
    {
95
        if ($dec <= 0x7F) {
1✔
96
            return chr($dec);
1✔
97
        }
98
        if ($dec <= 0x7FF) {
1✔
99
            return chr(($dec >> 6) + 192) . chr(($dec & 63) + 128);
1✔
100
        }
101
        if ($dec <= 0xFFFF) {
1✔
102
            return chr(($dec >> 12) + 224) . chr((($dec >> 6) & 63) + 128) . chr(($dec & 63) + 128);
1✔
103
        }
104
        if ($dec <= 0x1FFFFF) {
1✔
105
            return chr(($dec >> 18) + 240) . chr((($dec >> 12) & 63) + 128) . chr((($dec >> 6) & 63) + 128) . chr(($dec & 63) + 128);
1✔
106
        }
107

108
        return '';
1✔
109
    }
110

111
    /**
112
     * Convert from OpenXML escaped control character to PHP control character.
113
     *
114
     * @param string $value Value to unescape
115
     *
116
     * @return string
117
     */
118
    public static function controlCharacterOOXML2PHP($value = '')
119
    {
120
        if (empty(self::$controlCharacters)) {
1✔
UNCOV
121
            self::buildControlCharacters();
×
122
        }
123

124
        return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $value);
1✔
125
    }
126

127
    /**
128
     * Check if a string contains UTF-8 data.
129
     *
130
     * @param string $value
131
     *
132
     * @return bool
133
     */
134
    public static function isUTF8($value = '')
135
    {
136
        return is_string($value) && ($value === '' || preg_match('/^./su', $value) == 1);
317✔
137
    }
138

139
    /**
140
     * Return UTF8 encoded value.
141
     *
142
     * @param null|string $value
143
     *
144
     * @return ?string
145
     */
146
    public static function toUTF8($value = '')
147
    {
148
        if (null !== $value && !self::isUTF8($value)) {
325✔
149
            // PHP8.2 : utf8_encode is deprecated, but mb_convert_encoding always usable
150
            $value = (function_exists('mb_convert_encoding')) ? mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1') : utf8_encode($value);
11✔
151
        }
152

153
        return $value;
325✔
154
    }
155

156
    /**
157
     * Returns unicode from UTF8 text.
158
     *
159
     * The function is splitted to reduce cyclomatic complexity
160
     *
161
     * @param string $text UTF8 text
162
     *
163
     * @return string Unicode text
164
     *
165
     * @since 0.11.0
166
     */
167
    public static function toUnicode($text)
168
    {
169
        return self::unicodeToEntities(self::utf8ToUnicode($text));
3✔
170
    }
171

172
    /**
173
     * Returns unicode array from UTF8 text.
174
     *
175
     * @param string $text UTF8 text
176
     *
177
     * @return array
178
     *
179
     * @since 0.11.0
180
     * @see http://www.randomchaos.com/documents/?source=php_and_unicode
181
     */
182
    public static function utf8ToUnicode($text)
183
    {
184
        $unicode = [];
3✔
185
        $values = [];
3✔
186
        $lookingFor = 1;
3✔
187

188
        // Gets unicode for each character
189
        for ($i = 0; $i < strlen($text); ++$i) {
3✔
190
            $thisValue = ord($text[$i]);
3✔
191
            if ($thisValue < 128) {
3✔
192
                $unicode[] = $thisValue;
3✔
193
            } else {
194
                if (count($values) == 0) {
1✔
195
                    $lookingFor = $thisValue < 224 ? 2 : 3;
1✔
196
                }
197
                $values[] = $thisValue;
1✔
198
                if (count($values) == $lookingFor) {
1✔
199
                    if ($lookingFor == 3) {
1✔
200
                        $number = (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64);
1✔
201
                    } else {
202
                        $number = (($values[0] % 32) * 64) + ($values[1] % 64);
1✔
203
                    }
204
                    $unicode[] = $number;
1✔
205
                    $values = [];
1✔
206
                    $lookingFor = 1;
1✔
207
                }
208
            }
209
        }
210

211
        return $unicode;
3✔
212
    }
213

214
    /**
215
     * Returns entites from unicode array.
216
     *
217
     * @param array $unicode
218
     *
219
     * @return string
220
     *
221
     * @since 0.11.0
222
     * @see http://www.randomchaos.com/documents/?source=php_and_unicode
223
     */
224
    private static function unicodeToEntities($unicode)
225
    {
226
        $entities = '';
3✔
227

228
        foreach ($unicode as $value) {
3✔
229
            if ($value != 65279) {
3✔
230
                $entities .= $value > 127 ? '\uc0{\u' . $value . '}' : chr($value);
3✔
231
            }
232
        }
233

234
        return $entities;
3✔
235
    }
236

237
    /**
238
     * Return name without underscore for < 0.10.0 variable name compatibility.
239
     *
240
     * @param string $value
241
     *
242
     * @return string
243
     */
244
    public static function removeUnderscorePrefix($value)
245
    {
246
        if (null !== $value) {
310✔
247
            if (substr($value, 0, 1) == '_') {
310✔
248
                $value = substr($value, 1);
1✔
249
            }
250
        }
251

252
        return $value;
310✔
253
    }
254
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc