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

PHPOffice / PHPWord / 6190005322

14 Sep 2023 07:42PM UTC coverage: 81.565% (-0.01%) from 81.575%
6190005322

push

github

web-flow
Merge pull request #2472 from PHPOffice/pr2447

Remove deprecated utf8_encode in PHP 8.2

6 of 6 new or added lines in 2 files covered. (100.0%)

10995 of 13480 relevant lines covered (81.57%)

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

18
namespace PhpOffice\PhpWord\Shared;
19

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

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

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

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

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

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

107
        return '';
1✔
108
    }
109

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

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

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

138
    /**
139
     * Return UTF8 encoded value.
140
     *
141
     * @param string $value
142
     *
143
     * @return string
144
     */
145
    public static function toUTF8($value = '')
146
    {
147
        if (null !== $value && !self::isUTF8($value)) {
234✔
148
            if (PHP_VERSION_ID < 80200) {
11✔
149
                $value = utf8_encode($value);
11✔
150
            } else {
151
                $value = mb_convert_encoding($value, 'UTF-8', mb_list_encodings());
×
152
            }
153
        }
154

155
        return $value;
234✔
156
    }
157

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

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

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

213
        return $unicode;
3✔
214
    }
215

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

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

236
        return $entities;
3✔
237
    }
238

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

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