Php: Split Multibyte String (Word) into Separate Characters

PHP: Split multibyte string (word) into separate characters

try a regular expression with 'u' option, for example

  $chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);

php break string into characters

There is a function for this: str_split

$broken = str_split("OVERFLOW", 1);

If your string can contain multi byte characters, use preg_split instead:

$broken = preg_split('##u', 'OVERFLOW', -1, PREG_SPLIT_NO_EMPTY);

How to split a string by multiple delimiters in PHP?

<?php

$pattern = '/[;,]/';

$string = "something here ; and there, oh,that's all!";

echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';

Convert a String into an Array of Characters

You will want to use str_split().

$result = str_split('abcdef');

http://us2.php.net/manual/en/function.str-split.php

PHP: Split string into array, like explode with no delimiter

$array = str_split("0123456789bcdfghjkmnpqrstvwxyz");

str_split takes an optional 2nd param, the chunk length (default 1), so you can do things like:

$array = str_split("aabbccdd", 2);

// $array[0] = aa
// $array[1] = bb
// $array[2] = cc etc ...

You can also get at parts of your string by treating it as an array:

$string = "hello";
echo $string[1];

// outputs "e"

separate string in two by given position

You can use substr to get the two sub-strings:

$str1 = substr($str, 0, $pos);
$str2 = substr($str, $pos);

If you omit the third parameter length, substr takes the rest of the string.

But to get your result you actually need to add one to $pos:

$string = 'Some string';
$pos = 5;
$begin = substr($string, 0, $pos+1);
$end = substr($string, $pos+1);

How to split string with special character (�) using PHP

Take a look at mb_split:

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

Split a multibyte string using regular expression pattern and returns
the result as an array.

Like this:

$string = "a�b�k�e";
$chunks = mb_split("�", $string);
print_r($chunks);

Outputs:

Array
(
[0] => a
[1] => b
[2] => k
[3] => e
)

How to separate exact characters based on accents?

Hope is's help:

$str = 'PHP';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);

Output:

Array
(
[0] => P
[1] => H
[2] => P
)


Related Topics



Leave a reply



Submit