Meadows of wild horses

Blog...

PHP

| Comments

str_split_unicode(), mb_strlen, mb_substr

한글 3줄 개행제한 및 10자 자르기 코드

split_word.php
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
<?
$Val1     = "가나다라마바사아아아아아\n아자차카야제커거\n미나다가라서여";
$word     = split("\n", $Val1);
$arr_cnt  = count($word);
$full_arr = array();

for ($i = 0; $i <= $arr_cnt-1; $i++) {
  $str = str_split_unicode($word[$i], 10);

  if ($full_arr) {
    $full_arr = array_merge((array)$full_arr, (array)$str);
  }
  else {
    $full_arr = array_merge((array)$str);
  }
}

if (count($full_arr) >= 4) {
  echo "Error"."\n";
  exit;
}

print_r($full_arr);

function str_split_unicode($str, $l) {
  $ret = array();
  $len = mb_strlen($str, "UTF-8");

  for ($i = 0; $i < $len; $i += $l) {
    $ret[] = mb_substr($str, $i, $l, "UTF-8");
  }

  return $ret;
}
?>

Comments