Skip to content

Instantly share code, notes, and snippets.

@tfevens
tfevens / AppServiceProvider.php
Created June 17, 2022 12:18
Capture all queries in a log file.
// Add this in the register() method.
// Credit to @trovster -> https://twitter.com/trovster/status/1537082202709803008
if (! $this->app->isProduction()) {
DB::listen(function ($query): void {
$date = Carbon::today()->toDateString();
$filepath = storage_path("logs/queries-{$date}.log");
File::append(
$filepath,
$query->sql . '[' . implode(', ', $query->bindings) . ']' . PHP_EOL
@tfevens
tfevens / Estimated Reading Time
Last active May 5, 2021 16:47
This snippet returns an integer, for the estimated reading time for a piece of text. Think of the avg time on most blog posts.
Str::macro('readingTime', function (...$text) {
$totalWords = str_word_count(implode(" ", $text));
$minutesToRead = round($totalWords / 200); // Avg WPM of adult
return (int)max(1, $minutesToRead);
});
function words($value, $words = 100, $end = '...')
{
preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
if ( ! isset($matches[0]) || strlen($value) === strlen($matches[0])) return $value;
return rtrim($matches[0]).$end;
}
/*
Usage:
@tfevens
tfevens / gist:4505057bec1f4a1d0b02
Created December 17, 2014 13:53
Simple function to swap an image on click.
$('img.swap').click(function() {
var src = ($(this).attr('src'));
var swap = ($(this).data('swap'));
$(this).attr('src', swap );
$(this).data('swap', src );
});
// <img src="images/pic1.jpg" data-swap="images/pic2.jpg" class="swap">
<?php
for($i=1;$i<=100;$i++) {
if (($i % 3) == 0 AND ($i % 5) != 0) {
echo 'Fizz'."<br>";
}
elseif (($i%3)!=0 AND ($i%5)==0) {
echo 'Buzz'."<br>";
}
elseif (($i%3)==0 AND ($i%5)==0) {