Emulate str_pad() with while loop and strlen function in PHP
Description
The following code shows how to emulate str_pad() with while loop and strlen function.
Example
/*w w w . j a va2 s .c om*/
<!DOCTYPE html>
<html>
<body>
<h1></h1>
<?php
$myString = "Hello, world!";
$desiredLength = 20;
echo "<pre>Original string: '$myString'</pre>";
while ( strlen( $myString ) < 20 ) {
$myString .= " ";
}
echo "<pre>Padded string: '$myString'</pre>";
?>
</body>
</html>
The code above generates the following result.