Skip to content

Commit 5971be2

Browse files
committed
Changes
1 parent 9a3ca08 commit 5971be2

File tree

6 files changed

+462
-141
lines changed

6 files changed

+462
-141
lines changed

README.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,19 @@ PHP class to convert files encoding
33

44
## Usage
55
```php
6-
construct($encoding_to = 'UTF-8', $encodings_detected = 'UTF-8,ISO-8859-1,WINDOWS-1252');
7-
EncodeFile($fileR, $fileW, &$encoding_original, &$encoding_final);
6+
EncodeFile($fileR, $encoding_to, $encodings_detected);
87
```
98

109
This is an example:
1110

1211
```php
13-
include 'src/codification.php';
14-
1512
$codification = new Codification();
1613

17-
$fileR = "file.txt";
18-
$fileW = 'encodedFile.txt;
19-
$encoding_original = "";
20-
$encoding_final = "";
21-
22-
$result = $codification->EncodeFile($fileR, $fileW, $encoding_original, $encoding_final);
14+
$fileR = 'file.txt';
15+
$encoding_to = 'UTF-8';
16+
$encodings_detected = 'UTF-8,ISO-8859-1,WINDOWS-1252';
2317

24-
($result) ? 'True' : 'False';
25-
echo "Original encoding: " . $encoding_original;
26-
echo "Final encoding: " . $encoding_final;
18+
$result = $codification->EncodeFile($fileR, $encoding_to, $encodings_detected);
2719
```
2820

2921
## Encoding values

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"name": "descom/php-codification",
2+
"name": "Descom/File_Encoding",
33
"description": "PHP class to encode files",
44
"type": "library",
55
"license": "MIT",
66
"keywords": ["php", "encoding", "encode", "convert file encoding", "UTF-8", "ISO-8859-1"],
7-
"homepage": "https://www.descomsms.com",
7+
"homepage": "https://www.descom.es",
88
"authors": [
99
{
1010
"name": "Juan José Llorens",
@@ -16,15 +16,15 @@
1616
},
1717
"autoload": {
1818
"psr-4": {
19-
"Descom\\Codification\\": "src/"
19+
"Descom\\File_Encoding\\": "src"
2020
}
2121
},
2222
"require-dev": {
2323
"phpunit/phpunit": "^5.7"
2424
},
2525
"autoload-dev": {
2626
"psr-4": {
27-
"Descom\\Codification\\Test\\": "tests"
27+
"Descom\\File_Encoding\\Test\\": "tests"
2828
}
2929
},
3030
"scripts": {

src/Codification.php

Lines changed: 0 additions & 124 deletions
This file was deleted.

src/FileEncoding.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
namespace Descom\File_Encoding;
3+
4+
class FileEncoding
5+
{
6+
/**
7+
* Encode file.
8+
*
9+
* @param string $fileR Original file
10+
* @param string &$encoding_to Encoding to encode file
11+
* @param string &$encodings_detected Ordered list of encodings
12+
*
13+
* @return bool
14+
*/
15+
public function encodeFile($file, $encoding_to = 'UTF-8', $encodings_detected = 'UTF-8,ISO-8859-1,WINDOWS-1252')
16+
{
17+
$encoding_original = $this->detectEncoding($file, $encodings_detected);
18+
if ($encoding_original != $encoding_to) {
19+
try {
20+
$fileW = '.temp';
21+
$handleR = @fopen($file, 'r');
22+
$handleW = @fopen($fileW, 'w');
23+
if ($handleR && $handleW) {
24+
while ($line = fgets($handleR, 4096)) {
25+
fwrite($handleW, mb_convert_encoding($line, $encoding_to, $encoding_original), 4096);
26+
}
27+
fclose($handleR);
28+
fclose($handleW);
29+
unlink($file);
30+
rename($fileW, $file);
31+
32+
}
33+
else{
34+
fclose($handleR);
35+
fclose($handleW);
36+
return false;
37+
}
38+
} catch (Exception $e) {
39+
return false;
40+
}
41+
}
42+
return $this->checkEncoding($file, $encoding_to);
43+
}
44+
45+
/**
46+
* Detect file encoding.
47+
*
48+
* @param string $fileR Original file
49+
* @param string $encodings_detected Ordered list of encodings
50+
*
51+
* @return string File encoding
52+
*/
53+
public function detectEncoding($fileR, $encodings_detected)
54+
{
55+
$encodings = explode(',', $encodings_detected);
56+
$handleR = @fopen($fileR, 'r');
57+
if ($handleR && count($encodings)) {
58+
while ($line = fgets($handleR, 4096)) {
59+
$encoding = mb_detect_encoding($line, $encodings_detected, true);
60+
if ($encoding != $encodings[0]) {
61+
fclose($handleR);
62+
return $encoding;
63+
}
64+
}
65+
fclose($handleR);
66+
return $encodings[0];
67+
}
68+
else
69+
return '';
70+
}
71+
72+
/**
73+
* Check if file encoding matches encoding_to.
74+
*
75+
* @param string $fileR Original file
76+
*
77+
* @return bool
78+
*/
79+
public function checkEncoding($fileR, $encoding_to)
80+
{
81+
$handleR = @fopen($fileR, 'r');
82+
83+
if ($handleR) {
84+
while ($line = fgets($handleR, 4096)) {
85+
if (!mb_check_encoding($line, $encoding_to)) {
86+
fclose($handleR);
87+
return false;
88+
}
89+
}
90+
fclose($handleR);
91+
}
92+
else
93+
return false;
94+
95+
return true;
96+
}
97+
}

0 commit comments

Comments
 (0)