Multibyte String
在线手册:中文 英文
PHP手册

Function Overloading Feature

You might often find it difficult to get an existing PHP application to work in a given multibyte environment. This happens because most PHP applications out there are written with the standard string functions such as substr(), which are known to not properly handle multibyte-encoded strings.

mbstring supports a 'function overloading' feature which enables you to add multibyte awareness to such an application without code modification by overloading multibyte counterparts on the standard string functions. For example, mb_substr() is called instead of substr() if function overloading is enabled. This feature makes it easy to port applications that only support single-byte encodings to a multibyte environment in many cases.

To use function overloading, set mbstring.func_overload in php.ini to a positive value that represents a combination of bitmasks specifying the categories of functions to be overloaded. It should be set to 1 to overload the mail() function. 2 for string functions, 4 for regular expression functions. For example, if it is set to 7, mail, strings and regular expression functions will be overloaded. The list of overloaded functions are shown below.

Functions to be overloaded
value of mbstring.func_overload original function overloaded function
1 mail() mb_send_mail()
2 strlen() mb_strlen()
2 strpos() mb_strpos()
2 strrpos() mb_strrpos()
2 substr() mb_substr()
2 strtolower() mb_strtolower()
2 strtoupper() mb_strtoupper()
2 stripos() mb_stripos()
2 strripos() mb_strripos()
2 strstr() mb_strstr()
2 stristr() mb_stristr()
2 strrchr() mb_strrchr()
2 substr_count() mb_substr_count()
4 ereg() mb_ereg()
4 eregi() mb_eregi()
4 ereg_replace() mb_ereg_replace()
4 eregi_replace() mb_eregi_replace()
4 split() mb_split()

Note:

It is not recommended to use the function overloading option in the per-directory context, because it's not confirmed yet to be stable enough in a production environment and may lead to undefined behaviour.


Multibyte String
在线手册:中文 英文
PHP手册
PHP手册 - N: Function Overloading Feature

用户评论:

zeddix at freenet dot de (15-May-2011 06:43)

Here a small helpful function to convert a php file for multi byte use:
<?php
    $filename
= 'header.php'; // File name
   
   
$search[] = 'mail(';    $replace[] = 'mb_send_mail(';
   
$search[] = 'strlen(';    $replace[] = 'mb_strlen(';
   
$search[] = 'strpos(';    $replace[] = 'mb_strpos(';
   
$search[] = 'strrpos(';    $replace[] = 'mb_strrpos(';
   
$search[] = 'substr(';    $replace[] = 'mb_substr(';
   
$search[] = 'strtolower(';    $replace[] = 'mb_strtolower(';
   
$search[] = 'strtoupper(';    $replace[] = 'mb_strtoupper(';
   
$search[] = 'stripos(';    $replace[] = 'mb_stripos(';
   
$search[] = 'strstr(';    $replace[] = 'mb_strstr(';
   
$search[] = 'stristr(';    $replace[] = 'mb_stristr(';
   
$search[] = 'strrchr(';    $replace[] = 'mb_strrchr(';
   
$search[] = 'substr_count(';    $replace[] = 'mb_substr_count(';
   
$search[] = 'ereg(';    $replace[] = 'mb_ereg(';
   
$search[] = 'eregi(';    $replace[] = 'mb_eregi(';
   
$search[] = 'ereg_replace(';    $replace[] = 'mb_ereg_replace(';
   
$search[] = 'eregi_replace(';    $replace[] = 'mb_eregi_replace(';
   
$search[] = 'split(';    $replace[] = 'mb_split(';
   
$file = file_get_contents($filename);
   
$file = str_replace($search,$replace,$file);
   
file_put_contents($filename,$file);
?>

Oliver Baltz (26-Jul-2010 10:25)

In case you need to (de)activate the overloading for a specific directory, try setting an appropriate php_admin_value in your httpd.conf, e.g.

<Directory ...>
   ...
   php_admin_value mbstring.func_overload 7
</Directory>

I'm not 100% sure if one can rely on that, but it seems to work for me.

andrew (21-May-2009 10:38)

It seems in php 5.2.7 - 5.2.9 under windows mbstring overloading not working in .htaccess files. Apache v.2.2.
Works only in php.ini, this is not convinient in some situations.