(php)有没有一种方法可以根据stdout上占用的“空格”来了解字符串的长度?

问题描述 投票:0回答:1

我需要知道在标准输出上打印时一个字符串占用了多少个“空格”。

因此,例如

<?
function customlength($input) {
    echo $input . "(" . strlen($input) . ")\n";
}
echo customlength("Stefano");
echo customlength("Stefano\t\t1");
echo str_repeat("---------+", 3) . "\n";
?>

让我知道2字符串的长度为7和10个字符,但在stdout上它们占用了7和17个“空格”

Stefano(7)
Stefano         1(10)
---------+---------+---------+

因此期望得到了

Stefano(7)
Stefano         1(17)
---------+---------+---------+

我需要知道在标准输出上打印时,一个字符串占用了多少个“空格”。因此,作为示例函数customlength($ input){echo $ input。 “(”。strlen($ input)。“)\ n”; } echo ...

php string spaces strlen
1个回答
0
投票

制表符问题的解决方法是将它们替换为空格:

© www.soinside.com 2019 - 2024. All rights reserved.