开发者

XML-Output of the character ellipsis from filename

开发者 https://www.devze.com 2023-04-05 16:18 出处:网络
I like to print out the special character ellipsis \"…\" in XML. If it is hardcoded it works. But if I get that character from readdir(). It w开发者_运维问答on\'t work. Why?

I like to print out the special character ellipsis "…" in XML. If it is hardcoded it works. But if I get that character from readdir(). It w开发者_运维问答on't work. Why?

Code:

<?php

header('Content-Type: text/xml; charset=utf-8');

$maxnesting = 2;

echo "<root>";

initXMLDir("//somefolder");

function initXMLDir($target, $level = 0){
    global $maxnesting;

    $ignore = array("cgi-bin", ".", "..");

    if(is_dir($target) && $level < $maxnesting){
        if($dir = opendir($target)){
            while (($file = readdir($dir)) !== false){
                if(!in_array($file, $ignore)){
                    if(is_dir("$target/$file")){
                        echo "<object><name>".$file."</name>";
                        initXMLDir("$target/$file", ($level+1));
                        echo "</object>";
                    }
                    else{
                        echo "<object>".$file."</object>";
                    }
                }
            }
        }
        closedir($dir);
    }
}
echo "</root>";
?>

If I hardcode it like this and remove the character for example from the filename, it works.

echo "<object>…".$file."…</object>";

The error it prints out is.

An invalid character was found in text content.

Edit-Workaround:

So my solution or workaround for this problem. By combining this function I found here

function xml_character_encode($string, $trans='') {
    $trans=(is_array($trans)) ? $trans : get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
    foreach ($trans as $k=>$v) $trans[$k]= "&#".ord($k).";";
    return strtr($string, $trans);
}

and with

iconv(mb_detect_encoding($file, "auto"), 'UTF-8', $file);

I solved my problem. So basically I'm encoding all characters first which causes problem with iconv() so I can safely use that later.

So use it like this:

$file= xml_character_encode($file);
$file= iconv(mb_detect_encoding($file, "auto"), 'UTF-8', $file);

I tried to manually replace the character ellipsis because it seems it's the only special character that won't display properly with utf8_encode() and htmlspecialchars() (which are the only 2 functions I would need if ellipsis would display properly) but can't be done somehow with strtr().

0

精彩评论

暂无评论...
验证码 换一张
取 消