简体   繁体   中英

PHP, convert UTF-8 to ASCII 8-bit

I'm trying to convert a string from UTF-8 to ASCII 8-bit by using the iconv function. The string is meant to be imported into an accounting software (some basic instructions parsed accordingly to SIE standards).

What I'm running now:

iconv("UTF-8", "ASCII", $this->_output)

This works for accounting software #1, but software #2 complains about the encoding. Specified encoding by the standard is: IBM PC 8-bit extended ASCII (Codepage 437) .

My question is, what version of ASCII is PHP encoding my string into, and if other than specified - how can I encode the string accordingly to the standard specification?

try this for the software #2

iconv("UTF-8", "CP437", $this->_output);

Extended ASCII is not the same as plain ASCII. The first one maybe accepts ASCII, but the second software requires Extended ASCII - Codepage 437

see this link

I'm looking at this question and what has been posted as an answer and am very disappointed in what I find here in addition to what I have been able to glean so far from other sources such as the PHP documentation in terms of acceptable or better answers.

I have an input string that is a property of an object. The input is UTF-8 from a database and I am happy that it is good form and valid. Every indication is that this is true. It comes originally from a database where it is prepared and stored by a third party. I would prefer to not need to have to change the input string before it is processed by this function. After processing the string is displayed on a webpage with meta-charset of UTF-8. That I have already checked.

The input string has HTML entities which I want to preserve so the first thing is to decode the HTML entities. If someone has a better idea this operation could be moved from first place to the end of the function. In my way of thinking it should not matter in which sequence this takes place.

So this brings me back to the original question. How should one convert from UTF-8 to ASCII 8-bit using PHP? It really doesn't matter what you answer at this point because I have already started to go my own way, which is evident from the PHP code below.

Essentially what I have begun to do is programmatically decode UTF-8 as problems arise. One advantage of this is that I can substitute whatever I choose for each problem as it arises, but I'd really rather rely on the community.

    function decodedText($langObject, $keyString) {
    $decodeText = htmlspecialchars_decode($langObject->$keyString);
    
    //$decodeText = iconv("UTF-8", "ISO-8859-1//IGNORE", $decodeText);
    //$decodeText = iconv("UTF-8", "CP437", $decodeText);
    
    $decodeText = str_replace("\204", '"', $decodeText); // quote
    $decodeText = str_replace("\223", '"', $decodeText); // quote
    $decodeText = str_replace("\224", '"', $decodeText); // quote
    $decodeText = str_replace("\302", "", $decodeText); // first byte of a 2-byte utf-8
    if ("p14p1" == $keyString) {
        error_log("BEFORE:");
        error_log($langObject->$keyString);
        error_log(substr($langObject->$keyString, 592, 26));
        //error_log(mb_ord(substr($langObject->$keyString, 36)));
        error_log(ord(substr($langObject->$keyString, 592, 1)));
        error_log(decbin(ord(substr($langObject->$keyString, 592, 1))));
        error_log(decbin(ord(substr($langObject->$keyString, 593, 1))));
        error_log(decbin(ord(substr($langObject->$keyString, 594, 1))));
        error_log(decbin(ord(substr($langObject->$keyString, 595, 1))));
        error_log(decbin(ord(substr($langObject->$keyString, 596, 1))));
        error_log(decbin(ord(substr($langObject->$keyString, 597, 1))));
        error_log($decodeText);
        error_log("AFTER:");
    }
    return $decodeText;
}

Provide a better answer to the original question or ignore this as you wish. It is interesting that at this time this question has been viewed 37,000+ times and so far there has been essentially no helpful answers given. And the existing answers have a total of 13 upvotes. BTW, CP437 did not work for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM