Skip to content

Instantly share code, notes, and snippets.

@ozgurg
Last active March 17, 2024 04:22
Show Gist options
  • Save ozgurg/d03aad5e3f00afe516a490fbb18e7d31 to your computer and use it in GitHub Desktop.
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.
<?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
@ozgurg
Copy link
Author

ozgurg commented Jul 13, 2022

I turn this into a JavaScript library: https://github.com/ozgurg/pad-center

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment