問題なく動く模様
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php /** * 文字列を同じ長さに切りそろえる * 文字列 $s を半角=1, 全角=2 として $narrowLength で区切って配列で返す */ function mb_strwidth_split($s, $narrowLength) { $result = []; while (strlen($s)) { $lead = mb_strimwidth($s, 0, $narrowLength); $s = mb_substr($s, mb_strlen($lead)); array_push($result, $lead); } return $result; } $s = 'あい0123うえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわゐゑ123をん'; print_r(mb_strwidth_split($s, 21)); /* $ php -version PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies $ php mb_strwidth_split.php Array ( [0] => あい0123うえおかきく [1] => けこさしすせそたちつ [2] => てとなにぬねのはひふ [3] => へほまみむめもやゆよ [4] => らりるれろわゐゑ123を [5] => ん ) */ |