Skip to content

Commit 1b4db07

Browse files
authored
Improved character matching, updated tests and supporting files (#2)
* Changed regex to match characters and updated/fixed tests * Updating readme * Relocating bracketisation * Updating CONTRIBUTING * Added test rtf file generator
1 parent ef460a1 commit 1b4db07

15 files changed

Lines changed: 1559 additions & 1526 deletions

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ Thanks for checking out my project. I'm nowhere near a linguistics expert (or an
33
[email me](mailto:hello@jollyblueman.com) or make a PR if you think there's something you could add.
44

55
## Requirements
6-
* All changes must be compatible with PHP 7.4 and above
6+
* All changes must be compatible with PHP 7.4 and above
7+
* Compatible with phpcs PSR12 standard (excluding any pangrams in the test suite)

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PHP UTF8 To RTF
22
## Description
3-
A simple class for converting UTF8 characters to an RTF safe format in PHP.
3+
A simple PHP class for converting UTF8 characters to an RTF safe version in a string.
44

55
## Installation
66
`composer require tomwilford/php-utf8-to-rtf`
@@ -14,13 +14,13 @@ Instantiate the converter using:
1414
$converter = new CharacterConverter();
1515
```
1616

17-
To find and replace UTF8 characters in a long string use:
17+
To find and replace UTF8 characters with RTF safe versions in a long string use:
1818

1919
```php
2020
$convertedString = $converter->findAndReplace($string);
2121
```
2222

23-
Or, for shorter strings, simply convert the whole string:
23+
Or, for shorter strings and characters, simply convert the whole string:
2424

2525
```php
2626
$convertedString = $converter->convertStringToRtf($string);
@@ -32,19 +32,19 @@ You can also convert an array of UTF8 strings:
3232
$convertedArray = $converter->convertArrayToRtf($arrayOfStrings);
3333
```
3434

35-
Finally, if you need to do any additional processing on the UTF8 strings before converting,
36-
you can create an array of UTF8 strings using:
35+
Finally, if you need to do any additional processing on the UTF8 characters before converting,
36+
you can extract them as an array using:
3737
```php
38-
$arrayToConvert = $converter->locateWordsInString($string);
38+
$arrayToConvert = $converter->locateCharactersInString($string);
3939
```
4040

4141
## Testing
42-
Testing has been done with PHPUnit and a series of pangrams sourced online to try to best capture
42+
PHPUnit tests have been written using series of pangrams sourced online to try to best capture
4343
as many characters as possible in different languages being used in a natural way. Please see the
4444
[Pangrams trait](tests/Resources/Pangrams.php) for the pangram sources' credits.
4545

46-
Currently, there are four failing tests for `findAndReplace()` whereby some characters are ignored by the
47-
regex and not replaced during the conversion.
46+
There also is a short script that can [generate a test RTF file](tests/GenerateTestFile/generateTestFile.php) that
47+
can be opened in a word processor to verify the results.
4848

4949
## Contributing
5050
Contributions are welcome, please see [CONTRIBUTING](CONTRIBUTING.md) for more information.

src/CharacterConverter.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,64 +10,64 @@
1010
namespace Wilf\PhpUtf8ToRtf;
1111

1212
/**
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.
1414
*
1515
* @author Tom Wilford <hello@jollyblueman.com>
1616
*/
1717
class CharacterConverter implements CharacterConverterInterface
1818
{
1919
public function findAndReplace(string $string): string
2020
{
21-
$wordsToConvert = $this->locateWordsInString($string);
22-
$convertedWords = $this->convertArrayToRtf($wordsToConvert);
21+
$stringsToConvert = $this->locateCharactersInString($string);
22+
$convertedStrings = $this->convertArrayToRtf($stringsToConvert);
2323

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);
2626
}
2727

2828
return $string;
2929
}
3030

31-
public function locateWordsInString(string $string): array
31+
public function locateCharactersInString(string $string): array
3232
{
33-
preg_match_all("/(\p{L}*(?:[^ -~\n]+))/u", $string, $matches);
33+
preg_match_all("/[^\x00-\x7F]/u", $string, $matches);
3434

35-
$words = [];
35+
$characters = [];
3636
foreach ($matches[0] as $match) {
37-
foreach (explode(' ', $match) as $item) {
38-
$words[] = $item;
37+
if (!in_array($match, $characters)) {
38+
$characters[] = $match;
3939
}
4040
}
4141

42-
return $words;
42+
return $characters ;
4343
}
4444

45-
public function convertArrayToRtf(array $words): array
45+
public function convertArrayToRtf(array $strings): array
4646
{
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);
5151
}
5252
}
5353

54-
return $convertedWords;
54+
return $convertedStrings;
5555
}
5656

57-
public function convertStringToRtf(string $word): string
57+
public function convertStringToRtf(string $string): string
5858
{
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];
6363

64-
if ($code < 0x7f) {
65-
$rtf .= $char;
64+
if ($convertedCharacter < 0x7f) {
65+
$convertedString .= $character;
6666
} else {
67-
$rtf .= sprintf("\\u%d?", $code);
67+
$convertedString .= '{' . sprintf("\\u%d?", $convertedCharacter) . '}';
6868
}
6969
}
7070

71-
return $rtf;
71+
return $convertedString;
7272
}
7373
}

src/CharacterConverterInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ interface CharacterConverterInterface
1818
{
1919
public function findAndReplace(string $string): string;
2020

21-
public function locateWordsInString(string $string): array;
21+
public function locateCharactersInString(string $string): array;
2222

23-
public function convertArrayToRtf(array $words): array;
23+
public function convertArrayToRtf(array $strings): array;
2424

25-
public function convertStringToRtf(string $word): string;
25+
public function convertStringToRtf(string $string): string;
2626
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/*
3+
* Copyright (c) 2023. Tom Wilford <hello@jollyblueman.com>
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under the BSD-style license found in the
7+
* LICENSE file in the root directory of this source tree.
8+
*/
9+
10+
require __DIR__ . "/../../vendor/autoload.php";
11+
12+
$file = file_get_contents(__DIR__ . '/../Resources/Pangrams.php');
13+
$file = str_replace("<?php
14+
15+
namespace Resources;
16+
17+
/**
18+
* A list of pangrams for testing, made possible by:
19+
* - Richard Rutter (majority), https://clagnut.com/blog/2380
20+
* - gacontuyenchien1 (Vietnamese), https://10fastfingers.com/text/212157-Vietnamese-pangram
21+
*/
22+
trait Pangrams
23+
{", '', $file);
24+
$file = str_replace("}", '', $file);
25+
$file = str_replace(" public string $", "", $file);
26+
$file = str_replace(" = ", "", $file);
27+
28+
$array = explode('";', $file);
29+
$strings = [];
30+
foreach ($array as $item) {
31+
$parts = explode('"', $item);
32+
if (trim($parts[0])) {
33+
$strings[ucfirst(trim($parts[0]))] = $parts[1];
34+
}
35+
}
36+
37+
$converter = new \Wilf\PhpUtf8ToRtf\CharacterConverter();
38+
foreach ($strings as $language => $string) {
39+
$strings[$language] = $converter->findAndReplace($string);
40+
}
41+
42+
$rtf = "{rtf1
43+
{\par}";
44+
45+
foreach ($strings as $language => $string) {
46+
$rtf .= $language . '{\par}';
47+
$rtf .= $string . '{\par}{\par}';
48+
}
49+
50+
$rtf .= '
51+
}';
52+
53+
$filePath = __DIR__ . '/test.rtf';
54+
$fh = fopen($filePath, 'w');
55+
file_put_contents(__DIR__ . '/test.rtf', $rtf);
56+
fclose($fh);

0 commit comments

Comments
 (0)