**PHP**
$datearr = explode("/", $cutOff);
$month = $datearr[0];
$day = $datearr[1];
$year = $datearr[2];
$mainten = "MAINTENANCE";
$pad=' ';
$maint = str_pad($mainten, 20, $pad);
$string = $cduid . $maint . $inarea . $year . $month . $day . "\n";
I am trying to parse this string to a server and $maint has to be padded with spaces to the right. I have also tried.....
$datearr = explode("/", $cutOff);
$month = $datearr[0];
$day = $datearr[1];
$year = $datearr[2];
$mainten = "MAINTENANCE";
$maint = str_pad($mainten, 20);
$string = $cduid . $maint . $inarea . $year . $month . $day . "\n";
When I echo $string $maint only has 1 space on the right. If I replace $pad=' '; with $pad='.'; I get the correct result but I need for it to be spaces.
What am I missing here?
The str_pad() function is a built-in function in PHP and is used to pad a string to a given length. We can pad the input string by any other string up to a specified length. If we do not pass the other string to the str_pad() function then the input string will be padded by spaces.
The str_pad() function pads a string to a new length.
In HTML you can have only one space shown, but normally in source there are count of spaces, as you wish.
&nbps;
will not work with str_pad, because it has 6 characters (in HTML its only 1 character), but for str_pad it will fail.
There is only one way, how to do it, you have to pad some character (ie. ~
) and then replace it with
$maint = str_replace('~', ' ', str_pad($mainten, 20, '~')); // just use some character you know isn't in your string
This will 100% work.
When I echo $string $maint only has 1 space on the right.
The problem is that if you echo your string in HTML code, it will not show all the spaces. If you view source of that page you can see all the spaces added.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With