|
10 | 10 | namespace Wilf\PhpUtf8ToRtf; |
11 | 11 |
|
12 | 12 | /** |
13 | | - * Offers methods for locating and converting UTF8 strings to an RTF format. |
| 13 | + * Methods for locating and converting UTF8 strings to an RTF format. |
14 | 14 | * |
15 | 15 | * @author Tom Wilford <hello@jollyblueman.com> |
16 | 16 | */ |
17 | 17 | class CharacterConverter implements CharacterConverterInterface |
18 | 18 | { |
19 | 19 | public function findAndReplace(string $string): string |
20 | 20 | { |
21 | | - $wordsToConvert = $this->locateWordsInString($string); |
22 | | - $convertedWords = $this->convertArrayToRtf($wordsToConvert); |
| 21 | + $stringsToConvert = $this->locateCharactersInString($string); |
| 22 | + $convertedStrings = $this->convertArrayToRtf($stringsToConvert); |
23 | 23 |
|
24 | | - foreach ($convertedWords as $word => $convertedWord) { |
25 | | - $string = str_replace($word, '{' . $convertedWord . '}', $string); |
| 24 | + foreach ($convertedStrings as $originalString => $convertedString) { |
| 25 | + $string = str_replace($originalString, $convertedString, $string); |
26 | 26 | } |
27 | 27 |
|
28 | 28 | return $string; |
29 | 29 | } |
30 | 30 |
|
31 | | - public function locateWordsInString(string $string): array |
| 31 | + public function locateCharactersInString(string $string): array |
32 | 32 | { |
33 | | - preg_match_all("/(\p{L}*(?:[^ -~\n]+))/u", $string, $matches); |
| 33 | + preg_match_all("/[^\x00-\x7F]/u", $string, $matches); |
34 | 34 |
|
35 | | - $words = []; |
| 35 | + $characters = []; |
36 | 36 | foreach ($matches[0] as $match) { |
37 | | - foreach (explode(' ', $match) as $item) { |
38 | | - $words[] = $item; |
| 37 | + if (!in_array($match, $characters)) { |
| 38 | + $characters[] = $match; |
39 | 39 | } |
40 | 40 | } |
41 | 41 |
|
42 | | - return $words; |
| 42 | + return $characters ; |
43 | 43 | } |
44 | 44 |
|
45 | | - public function convertArrayToRtf(array $words): array |
| 45 | + public function convertArrayToRtf(array $strings): array |
46 | 46 | { |
47 | | - $convertedWords = []; |
48 | | - foreach ($words as $word) { |
49 | | - if (!array_key_exists($word, $convertedWords)) { |
50 | | - $convertedWords[$word] = $this->convertStringToRtf($word); |
| 47 | + $convertedStrings = []; |
| 48 | + foreach ($strings as $string) { |
| 49 | + if (!array_key_exists($string, $convertedStrings)) { |
| 50 | + $convertedStrings[$string] = $this->convertStringToRtf($string); |
51 | 51 | } |
52 | 52 | } |
53 | 53 |
|
54 | | - return $convertedWords; |
| 54 | + return $convertedStrings; |
55 | 55 | } |
56 | 56 |
|
57 | | - public function convertStringToRtf(string $word): string |
| 57 | + public function convertStringToRtf(string $string): string |
58 | 58 | { |
59 | | - $rtf = ""; |
60 | | - for ($i = 0; $i < mb_strlen($word, "UTF-8"); $i++) { |
61 | | - $char = mb_substr($word, $i, 1, "UTF-8"); |
62 | | - $code = unpack("N", mb_convert_encoding($char, "UCS-4BE", "UTF-8"))[1]; |
| 59 | + $convertedString = ""; |
| 60 | + for ($i = 0; $i < mb_strlen($string, "UTF-8"); $i++) { |
| 61 | + $character = mb_substr($string, $i, 1, "UTF-8"); |
| 62 | + $convertedCharacter = unpack("N", mb_convert_encoding($character, "UCS-4BE", "UTF-8"))[1]; |
63 | 63 |
|
64 | | - if ($code < 0x7f) { |
65 | | - $rtf .= $char; |
| 64 | + if ($convertedCharacter < 0x7f) { |
| 65 | + $convertedString .= $character; |
66 | 66 | } else { |
67 | | - $rtf .= sprintf("\\u%d?", $code); |
| 67 | + $convertedString .= '{' . sprintf("\\u%d?", $convertedCharacter) . '}'; |
68 | 68 | } |
69 | 69 | } |
70 | 70 |
|
71 | | - return $rtf; |
| 71 | + return $convertedString; |
72 | 72 | } |
73 | 73 | } |
0 commit comments