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

PHPOffice / PHPWord / 15122105038

19 May 2025 08:03PM UTC coverage: 96.024% (-0.7%) from 96.767%
15122105038

Pull #2778

github

web-flow
Merge d88877134 into 6ca8c9ff6
Pull Request #2778: WIP: SVG Image Type Support

28 of 129 new or added lines in 4 files covered. (21.71%)

106 existing lines in 4 files now uncovered.

12509 of 13027 relevant lines covered (96.02%)

35.2 hits per line

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

96.36
/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) {
40✔
39
            if ($i != 9 && $i != 10 && $i != 13) {
40✔
40
                $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
40✔
41
                $replace = chr($i);
40✔
42
                self::$controlCharacters[$find] = $replace;
40✔
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)) {
100✔
65
            self::buildControlCharacters();
40✔
66
        }
67

68
        return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $value);
100✔
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✔
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);
335✔
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)) {
344✔
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
            if (is_bool($value)) {
11✔
NEW
UNCOV
152
                $value = null;
×
153
            }
154
        }
155

156
        return $value;
344✔
157
    }
158

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

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

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

214
        return $unicode;
3✔
215
    }
216

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

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

237
        return $entities;
3✔
238
    }
239

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

255
        return $value;
321✔
256
    }
257
}
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