Skip to content

Instantly share code, notes, and snippets.

@TwistedAsylumMC
Created August 7, 2019 21:29
Show Gist options
  • Save TwistedAsylumMC/c645e00614884545a53dfcae760d97f9 to your computer and use it in GitHub Desktop.
Save TwistedAsylumMC/c645e00614884545a53dfcae760d97f9 to your computer and use it in GitHub Desktop.
A php script which adds borders around text! (Made for educational purposes)
<?php
declare(strict_types=1);
if(isset($_POST["text"])){
$text = htmlspecialchars($_POST["text"], ENT_QUOTES, "UTF-8");
if($text === ""){
echo "You must enter some text<hr>";
}else{
$lines = preg_split('/\r\n|[\r\n]/', $text);
$padding = (int)$_POST["padding"];
$border = $_POST["border"];
$chars = [
"N" => $border === "Single" ? "─" : "═",
"NE" => $border === "Single" ? "┐" : "╗",
"E" => $border === "Single" ? "│" : "║",
"SE" => $border === "Single" ? "┘" : "╝",
"S" => $border === "Single" ? "─" : "═",
"SW" => $border === "Single" ? "└" : "╚",
"W" => $border === "Single" ? "│" : "║",
"NW" => $border === "Single" ? "┌" : "╔"
];
$max = max(array_map(function($line) : int{
return strlen($line);
}, $lines));
echo "<pre>" . $chars["NW"] . str_repeat($chars["N"], $max + ($padding * 2)) . $chars["NE"] . PHP_EOL;
echo str_repeat($chars["W"] . str_repeat(" ", $max + ($padding * 2)) . $chars["E"] . PHP_EOL, $padding - 2 < 0 ? 0 : $padding - 2);
foreach($lines as $line){
$whitespace = ($max - strlen($line) < 0 ? 0 : (int)(($max - strlen($line)) / 2)) + $padding;
echo $chars["W"] . str_repeat(" ", $whitespace) . $line . str_repeat(" ", ($whitespace % 2 === 0 || strlen($line) === $max ? $whitespace : $whitespace + 1)) . $chars["E"] . PHP_EOL;
}
echo str_repeat($chars["W"] . str_repeat(" ", $max + ($padding * 2)) . $chars["E"] . PHP_EOL, $padding - 2 < 0 ? 0 : $padding - 2);
echo $chars["SW"] . str_repeat($chars["S"], $max + ($padding * 2)) . $chars["SE"] . PHP_EOL . "Padding: " . $padding . PHP_EOL . "Border: " . $border . PHP_EOL . "</pre><hr>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Border Tests</title>
</head>
<body>
<form name="apply-border" action="border-test.php" method="post">
Text: <textarea name="text"></textarea>
<br><br>
Padding: <input type="number" value="1" min="1" max="5" name="padding">
<br><br>
Single Line: <input type="radio" name="border" value="Single" checked="checked">
<br>
Double Line: <input type="radio" name="border" value="Double">
<br><br>
<button type="submit">Apply border</button>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment