utf8_tohtml: add $all param to encode everything (default false)

This is useful when every character needs to be converted to HTML entities, e.g. (email) `obfuscate()`. Respective test has also been added.
This commit is contained in:
Phy 2019-01-11 16:20:54 -05:00
parent ad1ea0895e
commit df81ca99ee
2 changed files with 9 additions and 2 deletions

View File

@ -11,6 +11,12 @@ class utf8_html_test extends DokuWikiTest {
$this->assertEquals(utf8_tohtml($in),$out);
}
function test_from_1byte_all(){
$in = 'a';
$out = 'a';
$this->assertEquals(utf8_tohtml($in, true),$out);
}
function test_from_2byte(){
$in = "\xc3\xbc";
$out = 'ü';

View File

@ -569,12 +569,13 @@ if(!function_exists('utf8_tohtml')){
* @link http://php.net/manual/en/function.utf8-decode.php
*
* @param string $str
* @param bool $all Encode non-utf8 char to HTML as well
* @return string
*/
function utf8_tohtml ($str) {
function utf8_tohtml($str, $all = false) {
$ret = '';
foreach (utf8_to_unicode($str) as $cp) {
if ($cp < 0x80)
if ($cp < 0x80 && !$all)
$ret .= chr($cp);
elseif ($cp < 0x100)
$ret .= "&#$cp;";