Php 如何每隔N个宽度向字符串插入一个字符?
如何每隔N个宽度向字符串插入一个字符 e、 gPhp 如何每隔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 +
@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
vsmb_strwidth
)在循环中使用mb_substr。我说的是宽度,而不是长度(例如mb_strlen
vsmb_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