Fixed line split

I am looking for ways to split a string of Unicode alphanumeric type into fixed lengths. eg:

    992000199821376 John Smith 20070603

and the array should look like this:

Array (
 [0] => 99,
 [1] => 2,
 [2] => 00019982,
 [3] => 1376,
 [4] => "John Smith",
 [5] => 20070603
) 

array data will be broken as follows:

    Array [0] - Account type - must be 2 characters long,
    Array [1] - Account status - must be 1 character long,
    Array [2] - Account ID - must be 8 characters long,
    Array [3] - Account settings - must be 4 characters long,
    Array [4] - User Name - must be 20 characters long,
    Array [5] - Join Date - must be 8 characters long.
+5
source share
4 answers

Or if you want to avoid preg:

$string = '992000199821376John Smith          20070603';
$intervals = array(2, 1, 8, 4, 20, 8);

$start = 0;
$parts = array();

foreach ($intervals as $i)
{
   $parts[] = mb_substr($string, $start, $i);

   $start += $i;
}
+2
source
    $s = '992000199821376        20070603';

    if (preg_match('~(.{2})(.{1})(.{8})(.{4})(.{20})(.{8})~u', $s, $match))
    {
        list (, $type, $status, $id, $settings, $name, $date) = $match;
    }
0

substr .

$accountDetails = "992000199821376John Smith          20070603";
$accountArray = array(substr($accountDetails,0,2),substr($accountDetails,2,1),substr($accountDetails,3,8),substr($accountDetails,11,4),substr($accountDetails,15,20),substr($accountDetails,35,8));

, , ( akond) - , , ( ). (, ).

0

It is not possible to split a unicode string as you ask.

Impossible if parts are invalid. Some code points are not allocated, for example: שׁ - 2 code points (and 4 bytes in UTF-8 and UTF-16), and you cannot split it because it is undefined.

When you work with Unicode, “character” is a very slippery term. There are code points, glyphs, etc. See http://www.utf8everywhere.org , part on "length of string" for more details.

0
source

All Articles