Skip to content

Commit

Permalink
Merge pull request #1 from byjg/1.0.7
Browse files Browse the repository at this point in the history
1.0.7
  • Loading branch information
byjg committed Feb 4, 2019
2 parents 34af517 + 0c5155a commit a1b360b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": ">5.7"
"phpunit/phpunit": "5.7.*|7.4.*"
},
"license": "MIT"
}
27 changes: 27 additions & 0 deletions src/FromUTF8.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class FromUTF8
/**
* Convert a text in UTF8 to ISO-8859-1 used in emails.
*
* @deprecated Use instead: toMimeEncodedWord
* @param string $text
* @param int $wrap
* @return string
Expand Down Expand Up @@ -74,6 +75,32 @@ public static function toIso88591Email($text, $wrap = 0)
}
}

/**
* RFC 2047
* https://sjohannes.wordpress.com/2009/05/18/utf-8-explained/
*
* @param $text
* @return string
*/
public static function toMimeEncodedWord($text)
{
$result = "";
for ($i = 0; $i < strlen($text); $i++) {
$decimal = ord($text[$i]);
if ($decimal > 127 || $decimal == 63) {
$result .= "=" . strtoupper(dechex($decimal));
continue;
}
$result .= $text[$i];
}

if ($result == $text) {
return $text;
}

return "=?utf-8?Q?" . str_replace(" ", "_", $result) . "?=";
}

/**
* Remove all accents from UTF8 Chars.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/src/FromUTF8Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ public function testToIso88591Email()
);
}

public function testToMimeEncodedWord()
{
$this->assertEquals(
"=?utf-8?Q?Libert=C3=A9_Egalit=C3=A9_Fraternit=C3=A9?=",
FromUTF8::toMimeEncodedWord("Liberté Egalité Fraternité")
);

$this->assertEquals(
"=?utf-8?Q?=C3=A1=C3=A9=C3=AD=C3=B3=C3=BA?=",
FromUTF8::toMimeEncodedWord("áéíóú")
);

$this->assertEquals(
"=?utf-8?Q?Test_=C5=A9=C5=A8?=",
FromUTF8::toMimeEncodedWord("Test ũŨ")
);

$this->assertEquals(
"=?utf-8?Q?=D0=AF=D0=BA_=D1=82=D0=B8_=D0=BF=D0=BE=D0=B6=D0=B8=D0=B2=D0=B0=D1=94=D1=88=3F?=",
FromUTF8::toMimeEncodedWord(base64_decode("0K/QuiDRgtC4INC/0L7QttC40LLQsNGU0Yg/?="))
);
}

public function testRemoveAccent()
{
$this->assertEquals(
Expand Down

0 comments on commit a1b360b

Please sign in to comment.