iconv does not convert to sjis tildes unless you do something like this
<?PHP
$string = 'where are the (~) (~) tildes?'; // This is what we start off with, you can put any string in here that contains problematic characters in utf8 format
echo ('this is what we start with = '.$string.'<BR />'); //print string at start
$conv_str = iconv('utf-8','shift-jis'.'//TRANSLIT',$string);
echo ('this is not working = '.$conv_str.'<BR />'); //Just to show that this is not working.
$rstring = preg_replace ('/~/','1bytetilde',$string); //replace before conversion
echo ('this is modified string here = '.$rstring.'<BR />'); //This is the modified string
$conv_str2 = iconv('utf-8','shift-jis'.'//TRANSLIT',$rstring); //convert modified string
$rereplace=chr(126); //$rereplace is now a one byte tilde in shift_jis
$rerstring = preg_replace ('/1bytetilde/',$rereplace,$conv_str2); //rereplace with tildes
echo ('this is the correct result = '.$rerstring.'<BR />'); //the correct result
?>
iconv 함수
소개
이 모듈은 iconv 문자셋 변환 기능을 사용하는 인터페이스를 가지고 있습니다. 이 모듈로, 로컬 문자셋을 유니코드 문자셋 등의 다른 문자셋으로 변환할 수 있습니다. 지원하는 문자셋은 시스템의 iconv 실행에 의존합니다. 몇몇 시스템에서는 iconv가 기대한대로 작동하지 않을 수 있는 점에 주의하십시오. » GNU libiconv 라이브러리를 설치하면 대부분의 경우에 좋은 결과를 얻을 수 있습니다.
PHP 5.0.0부터, 이 확장 모듈은 다중 언어 스크립트를 작성할 때 도움을 주는 다양한 실용적인 함수를 제공합니다. 새로운 기능에 대해서 아래의 섹션을 살펴보십시오.
요구 조건
최근의 POSIX 호환 시스템을 사용한다면 iconv 기능을 표준 C 라이브러리에서 지원하기 때문에, 추가로 필요한 것이 없습니다. 그렇지 않다면 » libiconv 라이브러리를 얻어서 시스템에 설치해야 합니다.
설치
이 모듈이 제공하는 함수를 사용하려면, 설정 옵션 --with-iconv[=DIR]을 사용하여 PHP 바이너리를 빌드해야 합니다.
Note: Windows® 사용자 주의 Windows® 환경에서 이 모듈을 사용하려면, PHP/Win32 바이너리 패키지에 들어 있는 iconv.dll, 또는 iconv-1.3.dll(4.2.1 이전) DLL 파일을 PATH 환경 변수로 지정한 디렉토리나 Windows®의 시스템 디렉토리에 놓아야 합니다.
실행시 설정
이 함수의 작동은 php.ini 설정에 영향을 받습니다.
| 이름 | 기본값 | 설정권한 | 변경점 |
|---|---|---|---|
| iconv.input_encoding | "ISO-8859-1" | PHP_INI_ALL | PHP 4.0.5부터 사용할 수 있습니다. |
| iconv.output_encoding | "ISO-8859-1" | PHP_INI_ALL | PHP 4.0.5부터 사용할 수 있습니다. |
| iconv.internal_encoding | "ISO-8859-1" | PHP_INI_ALL | PHP 4.0.5부터 사용할 수 있습니다. |
몇몇 시스템(IBM AIX 등)은 "ISO-8859-1" 대신에 "ISO8859-1"을 사용하기에, 이 값을 설정 옵션과 함수 매개변수로 사용합니다.
Note: 설정 옵션 iconv.input_encoding 는 현재 사용되지 않습니다.
자원형
이 확장은 리소스형을 정의하지 않습니다.
예약 상수
PHP 4.3.0부터 실행시에 이 확장 모듈에서 어느 iconv implementation을 사용할지 결정할 수 있습니다.
| 이름 | 형 | 설명 |
|---|---|---|
| ICONV_IMPL | string | implementation 이름 |
| ICONV_VERSION | string | implementation 버전 |
Note: 이 상수들을 사용하여 implementation 의존적인 스크립트를 작성하는 것은 절대 권장하지 않습니다.
PHP 5.0.0부터, 다음의 상수를 사용할 수 있습니다:
| 이름 | 형 | 설명 |
|---|---|---|
| ICONV_MIME_DECODE_STRICT | integer | iconv_mime_decode()에서 사용하는 비트마스크 |
| ICONV_MIME_DECODE_CONTINUE_ON_ERROR | integer | iconv_mime_decode()에서 사용하는 비트마스크 |
참고
참고: GNU Recode 함수.
Table of Contents
- iconv_get_encoding — 문자 인코딩 변환을 위한 현재 설정 상태를 문의
- iconv_mime_decode_headers — Decodes multiple MIME header fields at once
- iconv_mime_decode — Decodes a MIME header field
- iconv_mime_encode — Composes a MIME header field
- iconv_set_encoding — 문자 인코딩 변환을 설정 Set current setting for character encoding conversion
- iconv_strlen — Returns the character count of string
- iconv_strpos — Finds position of first occurrence of a needle within a haystack
- iconv_strrpos — Finds the last occurrence of a needle within a haystack
- iconv_substr — Cut out part of a string
- iconv — 원하는 문자 인코딩으로 변환한다
- ob_iconv_handler — 출력 버퍼 핸들러로 문자 엔코딩을 변환한다
iconv
15-May-2008 08:40
Just a little fix for the utf8_to_windows1255 function above. The original function doesn't convert English characters well:
function utf8_to_windows1255($utf8) {
$windows1255 = "";
$chars = preg_split("//",$utf8);
for ($i=1; $i<count($chars)-1; $i++) {
$prefix = ord($chars[$i]);
$suffix = ord($chars[$i+1]);
//print ("<p>$prefix $suffix");
if ($prefix==215) {
$windows1255 .= chr($suffix+80);
$i++;
}
elseif ($prefix==214) {
$windows1255 .= chr($suffix+16);
$i++;
}
else {
$windows1255 .= $chars[$i];
}
}
return $windows1255;
}
25-Mar-2007 12:32
Note that my mysql_iconv will not translate correctly the Hebrew dotting symbols (Niqqud) - they will be converted into question marks.
Here is a straightforward (and not very efficient) solution:
function utf8_to_windows1255($utf8) {
$windows1255 = "";
$chars = preg_split("//",$utf8);
for ($i=1; $i<count($chars)-1; $i+=2) {
$prefix = ord($chars[$i]);
$suffix = ord($chars[$i+1]);
print ("<p>$prefix $suffix");
if ($prefix==215)
$windows1255 .= chr($suffix+80);
elseif ($prefix==214)
$windows1255 .= chr($suffix+16);
else
$windows1255 .= $chars[$i];
}
return $windows1255;
}
13-Sep-2006 06:00
// Simple file translation.
$FileToconvert = "menu.xml";
$FileConverted = "menu2.xml";
echo "Converting $FileToconvert ...";
file_put_contents($FileConverted, iconv("ISO-8859-1","UTF-8",file_get_contents($FileToconvert)));
echo "File converted in $FileConverted";
17-Jul-2006 07:17
If you need convert string from Windows-1251 to 866. Some characters of 1251 haven't representation on DOS 866. For example, long dash -- chr(150) will be converted to 0, after that iconv finish his work and other charactes will be skiped. Problem characters range in win1251 (128-159,163,165-167,169,171-174,177-182,187-190).
Use this:
//$text - input text in windows-1251
//$cout - output text in 866 (cp866, dos ru ascii)
for($i=0;$i<strlen($text);$i++) {
$ord=ord($text[$i]);
if($ord>=192&&$ord<=239) $cout.=chr($ord-64);
elseif($ord>=240&&$ord<=255) $cout.=chr($ord-16);
elseif($ord==168) $cout.=chr(240);
elseif($ord==184) $cout.=chr(241);
elseif($ord==185) $cout.=chr(252);
elseif($ord==150||$ord==151) $cout.=chr(45);
elseif($ord==147||$ord==148||$ord==171||$ord==187) $cout.=chr(34);
elseif($ord>=128&&$ord<=190) $i=$i; //нет представления данному символу
else $cout.=chr($ord);
}
16-Mar-2006 04:22
There's one more special german character: ß (sometimes displayed as Ϋ)
so: case 159: $out .= "ß";break;
But this is a very slow method to convert this:
// function to change german umlauts into ue, oe, etc.
function cv_input($str){
Better try this:
$tr = array(chr(xyz) => '', chr(160) => ' '); // Just a simple example, put all your characters in there
$string = strtr($string, $tr);
27-Sep-2005 02:09
In addition to Godfather's note below, you may find this function useful just as well.
// function to change german umlauts into ue, oe, etc.
function cv_input($str){
$out = "";
for ($i = 0; $i<strlen($str);$i++){
$ch= ord($str{$i});
switch($ch){
case 195: $out .= "";break;
case 164: $out .= "ae"; break;
case 188: $out .= "ue"; break;
case 182: $out .= "oe"; break;
case 132: $out .= "Ae"; break;
case 156: $out .= "Ue"; break;
case 150: $out .= "Oe"; break;
default : $out .= chr($ch) ;
}
}
return $out;
}
15-Dec-2004 05:36
With this function you can translate the german Symbols from the character set UTF-8 in windows-1252.
function convert_text($str){
$out = '';
for ($i = 0; $i<strlen($str);$i++){
$ch = ord($str{$i});
switch($ch){
case 252: $out .= chr(129);break; //u Umlaut
case 220: $out .= chr(154);break;//U Umlaut
case 228: $out .= chr(132);break;//a Umlaut
case 196: $out .= chr(142);break;//A Umlaut
case 214: $out .= chr(153);break;//O Umlaut
case 246: $out .= chr(148);break;//o Umlaug
case 223: $out .= chr(225);break;//SZ
default : $out .= chr($ch) ;
}
}
return $out;
}
19-Aug-2004 01:40
iconv now has been built-in, at least in PHP >= 5.0.1 for win32. You don't have to modify php.ini for this. Actually you should not. And clearly, libiconv does not need to be installed.
23-Dec-2003 02:26
Windows users.
Personaly I leaved all php dlls in \php\dlls\ directory, just adding this path to my system path, and iconv.dll supplied with php 4.3.2 works fine, also leaving supplied php_iconv.dll in my \php\extensions\ directory. This was working fine with Apache and Omnihttpd server I use.
As soon I installed IIS on the same server, php complained about not finding php_iconv.dll in the extensions directory. In fact PHP with IIS loads all extensions in my \php\extensions directory correctly, except php_iconv.dll.
Although iconv.dll is in my system path, the only way to load php_iconv.dll was to copy iconv.dll file in \%winnt\system32 directory. With other servers, iconv.dll can be in anywhere in the system path.
I'm not sure how recent version of
glibc 2.x Slackware 7.x/8.x comes with, but
it's very likely that it comes with glibc 2.2.x.
In that case, you don't have to bother at all to
install libiconv in /usr/local. iconv(3) in glibc 2.2.x
is very good (thanks to Ulrich Drepper and
Bruno Haible. the latter is the author of libiconv).
libiconv is very handy for those outdated/non-standard-compliant Unix
and non-Unix systems that don't have
sufficiently good iconv(3) in their C library.
25-Jul-2002 08:39
To compile libiconv under Slackware 7.0 or 8.0 without errors (either with the apache module of PHP or the CGI version), you must specify the full path of the libiconv installation.
Exemple :
--with-iconv=/usr/local
