Last active
March 17, 2024 04:22
-
-
Save ozgurg/d03aad5e3f00afe516a490fbb18e7d31 to your computer and use it in GitHub Desktop.
It fills between two text with the given character until its length reach the given limit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function str_pad_center($prefix, $suffix, $length, $char) { | |
$countWithoutChar = strlen($prefix) + strlen($suffix); | |
if($countWithoutChar > $length) { | |
return ""; // In your case, you may want to change this | |
} | |
$fill = str_repeat($char, $length - $countWithoutChar); | |
return $prefix . $fill . $suffix; | |
} | |
echo str_pad_center("PR", "CO", 15, "_"); // PR___________CO / Length: 15 | |
echo str_pad_center("PR", "CO", 10, "_"); // PR______CO / Length: 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I turn this into a JavaScript library: https://github.com/ozgurg/pad-center