phputf-8character-encodingiconv

UTF8 to CP1255 conversion


I am trying to convert a UTF-8 string to CP1255 (Hebrew)

I have tried running the following (I'm using detect_encoding because some of my inputs are not UTF-8):

foreach($param as $key=>$value){
    $newval = iconv(mb_detect_encoding($value),"cp1255",$value);
    $querystr .= $key."=".$newval."&";
}

Anyway, the result is that all the Hebrew characters returns the nice � symbol, And all others(English/numbers) are as expected and wanted remain intact.

How can I do this properly?


Solution

  • I have found a solution:

    foreach($param as $key=>$value){
        $value_encoding = mb_detect_encoding($value);
        if($value_encoding == "UTF-8"){
            $newval = iconv($value_encoding,"cp1255",$value);
        }else{
            $newval = $value;
        }
        $endpoint = add_query_arg($key,$newval,$endpoint);
    }
    
    $content = file_get_contents($endpoint);