Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何每隔N个宽度向字符串插入一个字符?_Php - Fatal编程技术网

Php 如何每隔N个宽度向字符串插入一个字符?

Php 如何每隔N个宽度向字符串插入一个字符?,php,Php,如何每隔N个宽度向字符串插入一个字符 e、 g @haris shama是如何说的,它在 你需要这个: <?php $input = "テスaトテaストa"; $tempStr = ''; $count = 0; for ($i = 0; $i < strlen($input); $i++) { $currChar = $input[$i]; $countTemp = mb_strwidth($currChar); $count = $count +

如何每隔N个宽度向字符串插入一个字符

e、 g


@haris shama是如何说的,它在

你需要这个:

<?php
$input = "テスaトテaストa";

$tempStr = '';
$count = 0;

for ($i = 0; $i < strlen($input); $i++) {

    $currChar = $input[$i];
    $countTemp = mb_strwidth($currChar);

    $count = $count + $countTemp;

    if($count == 7){
        $tempStr = $tempStr.$currChar.'x';
        $count = 0;
    }
    else{
        $tempStr = $tempStr.$currChar;
    }
}    

echo $tempStr; // will print テスaxトテaxストax

@harishharma字长与字宽不同(例如
mb_strlen
vs
mb_strwidth
)在循环中使用mb_substr。我说的是宽度,而不是长度(例如
mb_strlen
vs
mb_strwidth
stru split
不能处理多字节字符。第一个:不,它不能正确处理多字节字符。第二个:正确处理多字节字符,但不处理字符宽度…
$parts = str_split($input, 5);
$final = implode("x", $parts);
<?php
$input = "テスaトテaストa";

$tempStr = '';
$count = 0;

for ($i = 0; $i < strlen($input); $i++) {

    $currChar = $input[$i];
    $countTemp = mb_strwidth($currChar);

    $count = $count + $countTemp;

    if($count == 7){
        $tempStr = $tempStr.$currChar.'x';
        $count = 0;
    }
    else{
        $tempStr = $tempStr.$currChar;
    }
}    

echo $tempStr; // will print テスaxトテaxストax
<?php
function split($str, $len = 1) {
    $arr        = array();

    $length     = mb_strlen($str, 'UTF-8');

    for ($i = 0; $i < $length; $i += $len) {
        $arr[] = mb_substr($str, $i, $len, 'UTF-8');
    }

    return $arr;
}

$input = "テスaトテaストa";

$parts = split($input, 3);
$final = implode("x", $parts).'x';

echo $final; // will print テスaxトテaxストax