Skip to content

Instantly share code, notes, and snippets.

View pistou's full-sized avatar
🤓

Pierre Skowron pistou

🤓
View GitHub Profile
@pistou
pistou / abstract.php
Last active July 13, 2016 10:15
Get N first characters of a string, without cutting off the last word
<?php
$nbChars = 50;
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in ullamcorper est. Quisque semper commodo maximus. Pellentesque pharetra libero lorem, at scelerisque eros sollicitudin ac. Phasellus ullamcorper sem nisi, in tristique augue tincidunt eu. Vivamus congue at felis at porttitor. Curabitur eget urna ac arcu pharetra blandit. Etiam non facilisis ante. Proin iaculis consequat leo sit amet faucibus. Nulla sed venenatis nisl, eget placerat dolor. Donec aliquam egestas dui vitae faucibus. Quisque eget suscipit mauris. Vivamus eget sem erat.";
echo abstract($text, $nbChars);
function abstract(string $text="", int $nbChars=50) {
$abstract = explode("\n", wordwrap($text, $nbChars));
return $abstract[0] . " ...";
}
?>
@pistou
pistou / urlTransformation.js
Last active January 12, 2022 10:53
URL Parameters transformation
const objectToString = (o) => {
return Object.keys(o)
.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(o[k])}`)
.join('&');
};
const obj = {foo: "bar", abc: "xyz", name: "John Doe"};
const str = objectToString(obj);
console.log(str); // foo=bar&abc=xyz&name=John%20Doe
@pistou
pistou / roundToPow.js
Last active January 21, 2021 14:10
Round number to closest power of X
function roundToPow(n, pow = 10) {
const p = Math.pow(pow, Math.round(n).toString().length - 1);
return Math.round(n / p) * p;
}
console.log(roundToPow(997)); // 1000
console.log(roundToPow(12457)); // 10000
console.log(roundToPow(13,3)); // 12