Skip to content

Instantly share code, notes, and snippets.

@lshaf
Last active February 19, 2016 13:43
Show Gist options
  • Save lshaf/8f90db0cb6e8f15565e9 to your computer and use it in GitHub Desktop.
Save lshaf/8f90db0cb6e8f15565e9 to your computer and use it in GitHub Desktop.
Words splitter
<?php
$text = isset($_POST['a']) ? $_POST['a'] : '';
$uname = isset($_POST['uname']) ? $_POST['uname'] : '';
$mode = isset($_POST['mode']) ? $_POST['mode'] : '';
$modeList = [
'kuroshi' => 'Kuroshi',
'shiba' => 'Shiba'
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="./jquery.js"></script>
<script src="./clipboard.js"></script>
</head>
<body>
<style>
textarea {
width: 100%;
resize: none;
}
div.area {
display: block;
cursor: pointer;
background-color: yellow;
word-spacing: normal;
word-wrap: break-word;
white-space: pre-wrap;
border: 1px solid #AAA;
font-family: 'Courier New';
font-size: 12px;
}
div.area.copied {
background-color: aqua;
}
</style>
<form action="" method="POST">
<textarea name="a" id="" rows="10"><?= $text ?></textarea><br>
<input type="text" name="uname" value="<?= $uname ?>">
<select name="mode">
<?php foreach ($modeList as $key => $value): ?>
<option value="<?= $key ?>" <?= $mode == $key ? 'selected' : '' ?>>
<?= $value ?>
</option>
<?php endforeach ?>
</select><br>
<input type="submit" value="Save">
</form><br><br>
<?php
if(isset($_POST['a'])){
$reslt = [];
$counter = 0;
if ($mode == 'shiba') {
$suffix = PHP_EOL."--";
$prefix = ($uname != '') ? $uname.PHP_EOL.'--'.PHP_EOL : '';
} else {
$suffix = "+";
$prefix = ($uname != '') ? $uname.PHP_EOL.PHP_EOL : '';
}
$limit = 140 - (strlen($suffix) + 1) - strlen($prefix);
$words = $text;
$eWord = explode(" ", $words);
foreach($eWord as $word){
$counter++;
$tmpdata[] = $word;
$all_str = implode(" ", $tmpdata);
if(strlen($all_str) > $limit){
$last_word = array_slice($tmpdata, 0, count($tmpdata) - 1);
$all_str = implode(" ", $last_word);
$reslt[] = $prefix.$all_str." {$suffix}";
$tmpdata = [$word];
}
if(count($eWord) == $counter){
$reslt[] = $prefix.$all_str;
}
}
$counter = 0;
foreach ($reslt as $str) {
# $counter += strlen($str);
# echo "[".str_pad(strlen($str), 3, '0', STR_PAD_LEFT)."]";
echo "<div class='area'>".$str."</div><br>";
}
}
?>
<script>
clip = new Clipboard('.area', {
text: function(trigger) {
return trigger.innerHTML;
}
});
clip.on('success', function(e) {
e.trigger.className += " copied";
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment