Skip to content

Instantly share code, notes, and snippets.

@AucT
AucT / trimWord.php
Last active December 29, 2022 10:46
trim entire words (remove words at start, end and both of the string)
<?php
function ltrimWord(string $str, array $words): string
{
$words = array_map('preg_quote', $words);
return preg_replace('/(' . implode('|', $words) . ')/A', '', $str);
}
function rtrimWord(string $str, array $words): string {
$words = array_map('preg_quote', $words);
@AucT
AucT / pluck.php
Created December 18, 2022 11:10
pluck array
<?php
function pluckObject($arrayOfObjects, string $value, ?string $key = null): array
{
$data = [];
if ($key) {
foreach ($arrayOfObjects as $object) {
$data[$object->{$key}] = $object->{$value};
}
} else {
@AucT
AucT / hugonew.ps1
Last active December 12, 2022 10:19
powershell script to create new post for hugo with good file name like 2022-12-12_my-awesome-title
## This is my file hugonew
##
## 1. Place this file where your helper scripts are. For example I have dropbox folder "my_scripts".
## Then add this folder to path variable.
## 2. cd to folder of your hugo blog and type:
## hugonew 'my awesome new nice title'
##
## this will create file 2022-12-12_my-awesome-new-nice-title.md in content/posts folder (if today is 2022-12-12)
## Some dummy ConvertTo-Slug function. scroll to bottom to see code.
@AucT
AucT / rename_files.php
Created December 11, 2022 15:44
copy this script to hugo content/posts folder and run php rename_files.php and you will get nicely named files with date prefix
<?php
//copy this script to hugo content/posts folder and run php rename_files.php and you will get nicely named files with date prefix
if (!function_exists('str_ends_with')) {
function str_ends_with(string $haystack, string $needle): bool
{
$needle_len = strlen($needle);
return ($needle_len === 0 || 0 === substr_compare($haystack, $needle, -$needle_len));
}
}
@AucT
AucT / str_contains.php
Created December 11, 2022 07:19
str_icontains and case-insensitive str_icontains
<?php
// Polyfill for PHP 4 - PHP 7, safe to utilize with PHP 8
if (!function_exists('str_contains')) {
function str_contains (string $haystack, string $needle)
{
return empty($needle) || strpos($haystack, $needle) !== false;
}
}
@AucT
AucT / echo_selected_checked_helper.php
Created November 28, 2022 11:53
php echo selected / checked if equal
<?php
function echoIfEqual($value1, $value2, $echoText) {
if ($value1 == $value2) echo $echoText;
}
function echoIfEqualChecked($value1, $value2) {
echoIfEqual($value1, $value2, 'checked');
}
function echoIfEqualSelected($value1, $value2) {
echoIfEqual($value1, $value2, 'selected');
@AucT
AucT / textarea_ctr_enter_submit_form.js
Created April 25, 2022 10:16
Pressing ctr+enter inside textarea will requestSubmitForm.
document.body.addEventListener('keydown', function (e) {
if (e.key === 'Enter' && e.target.localName === 'textarea' && (e.metaKey || e.ctrlKey) && e.target.form) {
e.target.form.requestSubmit();
}
});
@AucT
AucT / IndexNow.php
Created November 14, 2021 09:30
indexnow php example bing
<?php
class IndexNow
{
const LOG = true;
public static function submit(array $urls): void
{
$data = [
@AucT
AucT / arrayToHtmlTable.php
Created December 31, 2020 14:46
Get html table from array of assoc arrays (simple result set from mysql tables)
<?php
function arrayToTable(array $rows, string $tableAttributes = ''): string
{
if (!$rows) {
return '';
}
$rowsHeaders = array_keys($rows[0]);
$table = "<table {$tableAttributes}><tr>";
foreach ($rowsHeaders as $rowsHeader) {
@AucT
AucT / php_interval_to_minutes_seconds.php
Created December 27, 2020 11:10
Convert php Interval (ISO8601) to seconds function, Convert php Interval (ISO8601) to minutes function
<?php
function ISO8601ToSeconds($ISO8601){
$interval = new \DateInterval($ISO8601);
return ($interval->d * 24 * 60 * 60) +
($interval->h * 60 * 60) +
($interval->i * 60) +
$interval->s;
}
function ISO8601ToMinutes($ISO8601){