Home » Php » (php) Is there a way to know the string length in terms of "spaces" occupied on stdout?

(php) Is there a way to know the string length in terms of "spaces" occupied on stdout?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I need to know how many “spaces” a string is occupying when printed on stdout.

So, as example

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

is getting me that the 2 string are 7 and 10 characters long, but they took 7 and 17 “spaces” on stdout

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

So expectation is instead to get

Stefano(7)
Stefano         1(17)
---------+---------+---------+
How to&Answers: