This function allows you to pad a string with a specified string or character. It inserts the padding character or string between each character of the original string.
For example if your string is "Hello World" you could use this function to pad it with space characters and return "H e l l o W o r l d".
The function is called ExpandString and accepts two arguments, $str and $pad. $str is the string that needs to be expanded. $pad is the character or string that needs to be inserted between each character of $str.
Here is the function :
<?php
function ExpandString($str,$pad) {
$string = "";
// Copy each character from source string to
// output string but add a pad character.
for($i = 0; $i < strlen($str); $i++) {
$string = $string . $str[$i] . $pad;
}
// Remove trailing pad character
return trim($string,$pad);
}
?>
<?php
function ExpandString($str,$pad) {
$string = "";
// Copy each character from source string to
// output string but add a pad character.
for($i = 0; $i < strlen($str); $i++) {
$string = $string . $str[$i] . $pad;
}
// Remove trailing pad character
return trim($string,$pad);
}
echo "Expand Hello World with a space : ";
echo ExpandString("Hello World"," ");
echo "<br>";
echo "Expand 123456 with a dash : ";
echo ExpandString("123456","-");
echo "<br>";
echo "Expand ABCDEF with _-_ : ";
echo ExpandString("ABCDEF","_-_");
?>