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

search for in the

mb_strcut> <mb_send_mail
Last updated: Fri, 13 Nov 2009

view this page in

mb_split

(PHP 4 >= 4.2.0, PHP 5)

mb_splitマルチバイト文字列を正規表現により分割する

説明

array mb_split ( string $pattern , string $string [, int $limit = -1 ] )

マルチバイト文字列 string において、正規表現 pattern により文字列を分割し、 結果を配列として返します。

パラメータ

pattern

正規表現パターン。

string

分割する文字列。

limit
オプションの引数 limit を指定した場合は、 最大 limit 個の要素に分割されます。

返り値

結果を配列で返します。

注意

注意: 内部エンコーディングあるいは mb_regex_encoding() で指定した文字エンコーディングを、 この関数の文字エンコーディングとして使用します。

参考

  • mb_regex_encoding() - 現在の正規表現用のエンコーディングを文字列として返す
  • mb_ereg() - マルチバイト文字列に正規表現マッチを行う



mb_strcut> <mb_send_mail
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
mb_split
gert dot matern at web dot de
03-Aug-2009 10:34
We are talking about Multi Byte ( e.g. UTF-8) strings here, so preg_split will fail for the following string:

'Weiße Rosen sind nicht grün!'

And because I didn't find a regex to simulate a str_split I optimized the first solution from adjwilli a bit:

<?php
$string
= 'Weiße Rosen sind nicht grün!'
$stop   = mb_strlen( $string);
$result = array();

for(
$idx = 0; $idx < $stop; $idx++)
{
  
$result[] = mb_substr( $string, $idx, 1);
}
?>

Here is an example with adjwilli's function:

<?php
mb_internal_encoding
( 'UTF-8');
mb_regex_encoding( 'UTF-8'); 

function
mbStringToArray
( $string
)
{
 
$stop   = mb_strlen( $string);
 
$result = array();

  for(
$idx = 0; $idx < $stop; $idx++)
  {
    
$result[] = mb_substr( $string, $idx, 1);
  }

  return
$result;
}

echo
'<pre>', PHP_EOL,
print_r( mbStringToArray( 'Weiße Rosen sind nicht grün!', true)), PHP_EOL,
'</pre>';
?>

Let me know [by personal email], if someone found a regex to simulate a str_split with mb_split.
Sezer Yalcin
19-Feb-2009 01:13
To split by mb letters, use preg_split with /u modifier instead of calling mb functions thousand times.
adjwilli at yahoo dot com
26-Dec-2007 05:37
I figure most people will want a simple way to break-up a multibyte string into its individual characters. Here's a function I'm using to do that. Change UTF-8 to your chosen encoding method.

<?php
function mbStringToArray ($string) {
   
$strlen = mb_strlen($string);
    while (
$strlen) {
       
$array[] = mb_substr($string,0,1,"UTF-8");
       
$string = mb_substr($string,1,$strlen,"UTF-8");
       
$strlen = mb_strlen($string);
    }
    return
$array;
}
?>

mb_strcut> <mb_send_mail
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites