Skip to content

Instantly share code, notes, and snippets.

@ammardev
Created November 30, 2018 16:13
Show Gist options
  • Save ammardev/16521b747a678eb9e0665a905f13b22f to your computer and use it in GitHub Desktop.
Save ammardev/16521b747a678eb9e0665a905f13b22f to your computer and use it in GitHub Desktop.
<?php
# Problem 2:
# Function:
function listFramer(array $list): string {
$longestWord = 0;
foreach($list as $item)
if(($itemLength = strlen($item)) > $longestWord)
$longestWord = $itemLength;
$result = str_repeat('*', $longestWord + 4). PHP_EOL;
foreach($list as $item)
$result .= '* '. $item . str_repeat(' ', $longestWord-strlen($item)) . " *\n";
$result .= str_repeat('*', $longestWord + 4) . PHP_EOL;
return $result;
}
# Function Usage:
echo 'Insert a list of strings: ';
$stringList = fgets(fopen("php://stdin","r"));
$list = json_decode($stringList);
echo listFramer($list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment