PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mb_check_encoding> <マルチバイト文字列
Last updated: Fri, 05 Sep 2008

view this page in

マルチバイト文字列 関数

リファレンス

マルチバイト文字エンコーディングおよびそれに関連する問題は非常に複雑で、 このドキュメントの範囲を超えています。これらの問題に関連するより詳細な情報は、 以下の URL やその他のリソースを参照ください。

目次

  • mb_check_encoding — 文字列が、指定したエンコーディングで有効なものかどうかを調べる
  • mb_convert_case — 文字列に対してケースフォルディングを行う
  • mb_convert_encoding — 文字エンコーディングを変換する
  • mb_convert_kana — カナを("全角かな"、"半角かな"等に)変換する
  • mb_convert_variables — 変数の文字コードを変換する
  • mb_decode_mimeheader — MIME ヘッダフィールドの文字列をデコードする
  • mb_decode_numericentity — HTML 数値エンティティを文字にデコードする
  • mb_detect_encoding — 文字エンコーディングを検出する
  • mb_detect_order — 文字エンコーディング検出順序を設定あるいは取得する
  • mb_encode_mimeheader — MIMEヘッダの文字列をエンコードする
  • mb_encode_numericentity — 文字を HTML 数値エンティティにエンコードする
  • mb_ereg_match — マルチバイト文字列が正規表現に一致するか調べる
  • mb_ereg_replace — マルチバイト文字列に正規表現による置換を行う
  • mb_ereg_search_getpos — 次の正規表現検索を開始する位置を取得する
  • mb_ereg_search_getregs — マルチバイト文字列が正規表現に一致する部分があるか調べる
  • mb_ereg_search_init — マルチバイト正規表現検索用の文字列と正規表現を設定する
  • mb_ereg_search_pos — 指定したマルチバイト文字列が正規表現に一致する部分の位置と長さを返す
  • mb_ereg_search_regs — 指定したマルチバイト文字列が正規表現に一致する部分を取得する
  • mb_ereg_search_setpos — 次の正規表現検索を開始する位置を設定する
  • mb_ereg_search — 指定したマルチバイト文字列が正規表現に一致するか調べる
  • mb_ereg — マルチバイト文字列に正規表現マッチを行う
  • mb_eregi_replace — マルチバイト文字列に大文字小文字を区別せずに正規表現による置換を行う
  • mb_eregi — マルチバイト文字列に大文字小文字を区別しない正規表現マッチを行う
  • mb_get_info — mbstring の内部設定値を取得する
  • mb_http_input — HTTP 入力文字エンコーディングを検出する
  • mb_http_output — HTTP 出力文字エンコーディングを設定あるいは取得する
  • mb_internal_encoding — 内部文字エンコーディングを設定あるいは取得する
  • mb_language — 現在の言語を設定あるいは取得する
  • mb_list_encodings — サポートするすべてのエンコーディングの配列を返す
  • mb_output_handler — 出力バッファ内で文字エンコーディングを変換するコールバック関数
  • mb_parse_str — GET/POST/COOKIE データをパースし、グローバル変数を設定する
  • mb_preferred_mime_name — MIME 文字設定を文字列で得る
  • mb_regex_encoding — 現在の正規表現用のエンコーディングを文字列として返す
  • mb_regex_set_options — マルチバイト正規表現関数のデフォルトオプションを取得または設定する
  • mb_send_mail — エンコード変換を行ってメールを送信する
  • mb_split — マルチバイト文字列を正規表現により分割する
  • mb_strcut — 文字列の一部を得る
  • mb_strimwidth — 指定した幅で文字列を丸める
  • mb_stripos — 大文字小文字を区別せず、 文字列の中で指定した文字列が最初に現れる位置を探す
  • mb_stristr — 大文字小文字を区別せず、 文字列の中で指定した文字列が最初に現れる位置を探す
  • mb_strlen — 文字列の長さを得る
  • mb_strpos — 文字列の中に指定した文字列が最初に現れる位置を見つける
  • mb_strrchr — 別の文字列の中で、ある文字が最後に現れる場所を見つける
  • mb_strrichr — 大文字小文字を区別せず、 別の文字列の中である文字が最後に現れる場所を探す
  • mb_strripos — 大文字小文字を区別せず、 文字列の中で指定した文字列が最後に現れる位置を探す
  • mb_strrpos — 文字列の中に指定した文字列が最後に現れる位置を見つける
  • mb_strstr — 文字列の中で、指定した文字列が最初に現れる位置を見つける
  • mb_strtolower — 文字列を小文字にする
  • mb_strtoupper — 文字列を大文字にする
  • mb_strwidth — 文字列の幅を返す
  • mb_substitute_character — 置換文字を設定あるいは取得する
  • mb_substr_count — 部分文字列の出現回数を数える
  • mb_substr — 文字列の一部を得る


mb_check_encoding> <マルチバイト文字列
Last updated: Fri, 05 Sep 2008
 
add a note add a note User Contributed Notes
マルチバイト文字列 関数
patrick at hexane dot org
27-Jun-2008 09:18
I wonder why there isn't a mb_str_replace().  Here's one for now:

function mb_str_replace( $needle, $replacement, $haystack ) {
  $needle_len = mb_strlen($needle);
  $pos = mb_strpos( $haystack, $needle);
  while (!($pos ===false)) {
    $front = mb_substr( $haystack, 0, $pos );
    $back  = mb_substr( $haystack, $pos + $needle_len);
    $haystack = $front.$replacement.$back;
    $pos = mb_strpos( $haystack, $needle);
  }
  return $haystack;
}
tonyboyd
17-Oct-2007 12:52
JOECOLE, isn't this the same thing?

$str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
Smelly
25-Apr-2007 11:09
Below is some code to output a UTF-8 encoded CSV in a way understandable by Excel. It requires iconv instead of mbstring.

header("Content-type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=report.xls");
   
// assume $tmpString contains UTF-8 encoded CSV:
$tmpString =  iconv ( 'UTF-8', 'UTF-16LE//IGNORE', $tmpString );

print chr(255).chr(254).$tmpString;
chris at maedata dot com
24-Apr-2007 10:50
The opposite of what Eugene Murai wrote in a previous comment is true when importing/uploading a file. For instance, if you export an Excel spreadsheet using the Save As Unicode Text option, you can use the following to convert it to UTF-8 after uploading:

//Convert file to UTF-8 in case Windows mucked it up
$file = explode( "\n", mb_convert_encoding( trim( file_get_contents( $_FILES['file']['tmp_name'] ) ), 'UTF-8', 'UTF-16' ) );
mdoocy at u dot washington dot edu
14-Mar-2007 12:30
Note that some of the multi-byte functions run in O(n) time, rather than constant time as is the case for their single-byte equivalents. This includes any functionality requiring access at a specific index, since random access is not possible in a string whose number of bytes will not necessarily match the number of characters. Affected functions include: mb_substr(), mb_strstr(), mb_strcut(), mb_strpos(), etc.
motin at demomusic dot nu
16-Feb-2007 06:24
Follow up on last note from 2007-jan-20: http://se2.php.net/manual/en/function.mb-strlen.php#72979

There is the correct way of simulating singlebyte strlen as well as some pitfalls to watch out for when developing in a mb-func_overload:ed environment.
motin at demomusic dot nu
19-Jan-2007 06:12
As peter dot albertsson at spray dot se already pointed out, overloading strlen may break code that handles binary data and relies upon strlen for bytelengths.

The problem occurs when a file is filled with a string using fwrite in the following manner:

$len = strlen($data);
fwrite($fp, $data, $len);

fwrite takes amount of bytes as the third parameter, but mb_strlen returns the amount of characters in the string. Since multibyte characters are possibly more than one byte in length each - this will result in that the last characters of $data never gets written to the file.

After hours of investigating why PEAR::Cache_Lite didn't work - the above is what I found.

I made an attempt at using single byte functions, but it doesn't work. Posting here anyway in case it helps someone else:

/**
* PHP Singe byte functions simulation (non successful)
*
* Usage: sb_string(functionname, arg1, arg2, etc);
* Example: sb_string("strlen", "tuöéä"); returns 8 (should...)
*/
function sb_string() {

  $arguments = func_get_args();

  $func_overloading = ini_get("mbstring.func_overload");

  ini_set("mbstring.func_overload", 0);

  $ret = call_user_func_array(array_shift($arguments), $arguments);

  ini_set("mbstring.func_overload", $func_overloading);

  return $ret;

}
pdezwart .at. snocap
10-Oct-2006 12:28
If you are trying to emulate the UnicodeEncoding.Unicode.GetBytes() function in .NET, the encoding you want to use is: UCS-2LE
hayk at mail dot ru
17-Aug-2006 01:36
Since PHP 5.1.0 and PHP 4.4.2 there is an Armenian ArmSCII-8 (ArmSCII-8, ArmSCII8, ARMSCII-8, ARMSCII8) encoding avaliable.
daniel at softel dot jp
24-Jul-2006 05:41
Note that although "multi-byte" hints at total internationalization, the mb_ API was designed by a Japanese person to support the Japanese language.

Some of the functions, for example mb_convert_kana(), make absolutely no sense outside of a Japanese language environment.

It should perhaps be considered "lucky" if the functions work with non-Japanese multi-byte languages.

I don't mean any disrespect to the mb_ API because I'm using it everyday and I appreciate its usefulness, but maybe a better name would be the jp_ API.
Aardvark
13-Mar-2006 12:37
Since not all hosted servces currently support the multi-byte function set, it may still be necessary to process Unicode strings using standard single byte functions.  The function at the following link - http://www.kanolife.com/escape/2006/03/php-unicode-processing.html - shows by example how to do this.  While this only covers UTF-8, the standard PHP function "iconv" allows conversion into and out of UTF-8 if strings need to be input or output in other encodings.
peter kehl
09-Mar-2006 09:34
UTF-16LE solution for CSV for Excel by Eugene Murai works well:
$unicode_str_for_Excel = chr(255).chr(254).mb_convert_encoding( $utf8_str, 'UTF-16LE', 'UTF-8');

However, then Excel on Mac OS X doesn't identify columns properly and its puts each whole row in its own cell. In order to fix that, use TAB "\\t" character as CSV delimiter rather than comma or colon.

You may also want to use HTTP encoding header, such as
header( "Content-type: application/vnd.ms-excel; charset=UTF-16LE" );
14-Aug-2005 08:24
get the string octet-size, when mbstring.func_overload is set to 2 :

<?php
function str_sizeof($string) {
    return
count(preg_split("`.`", $string)) - 1 ;
}
?>

answering to peter albertsson, once you got your data octet-size, you can access each octet with something
$string[0] ... $string[$size-1], since the [ operator doesn't complies with multibytes strings.
peter dot albertsson at spray dot se
21-May-2005 04:43
Setting mbstring.func_overload = 2 may break your applications that deal with binary data.

After having set mbstring.func_overload = 2 and  mbstring.internal_encoding = UTF-8 I can't even read a binary file and print/echo it to output without corrupting it.
nzkiwi at NOSPAMmte dot biglobe dot ne dot jp
13-Apr-2005 05:37
A friend has pointed out that the entry
"mbstring.http_input PHP_INI_ALL" in Table 1 on the mbstring page appears to be wrong: above Example 4 it says that "There is no way to control HTTP input character conversion from PHP script. To disable HTTP input character conversion, it has to be done in php.ini".
Also the table shows the old-PHP-version defaults:
;; Disable HTTP Input conversion
mbstring.http_input = pass  *BUT* (for PHP 4.3.0 or higher)
;; Disable HTTP Input conversion
mbstring.encoding_translation = Off
Eugene Murai
23-Feb-2005 11:20
PHP can input and output Unicode, but a little different from what Microsoft means: when Microsoft says "Unicode", it unexplicitly means little-endian UTF-16 with BOM(FF FE = chr(255).chr(254)), whereas PHP's "UTF-16" means big-endian with BOM. For this reason, PHP does not seem to be able to output Unicode CSV file for Microsoft Excel. Solving this problem is quite simple: just put BOM infront of UTF-16LE string.

Example:

$unicode_str_for_Excel = chr(255).chr(254).mb_convert_encoding( $utf8_str, 'UTF-16LE', 'UTF-8');
Geoffrey
01-Feb-2005 01:59
For Windows users php_mbstring can be added as follows:-

if you have dowloaded  the "short" version of PHP,
(php-4.3.10-installer.exe), download the full version .
(php-4.3.10-Win32.zip)

unzip it, find php_mbstring.dll in
f:\php-4.3.10-Win32\extensions, and copy it across to your
php\extensions directory

use Notepad to open your PHP.INI

change the extension_dir line to read
extension_dir = "e:\php\extensions\"  (or whatever your
directory is called)

remove the semi-colon on line
 ; extension=php_mbstring.dll

save PHP.INI,  restart PHP

mb_check_encoding> <マルチバイト文字列
Last updated: Fri, 05 Sep 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites