Skip to content

Instantly share code, notes, and snippets.

View AlexPashley's full-sized avatar

Alex Pashley AlexPashley

View GitHub Profile
@AlexPashley
AlexPashley / helpers.js
Last active May 25, 2020 18:39
JS: Handlebars - Block Helpers #1 nl2br - Replace returns with <br> #2 If Greater than comparison operator #3 Find and replace function
// {{#nl2br}} replace returns with <br>
Handlebars.registerHelper('nl2br', function(options) {
var nl2br = (options.fn(this) + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br>' + '$2');
return new Handlebars.SafeString(nl2br);
});
/**
* {{#ifGt}} greater than helper
*
* @param1 int param1
@AlexPashley
AlexPashley / seo_slug.php
Last active December 18, 2015 23:19
PHP: Dynamic slug function with SEO improvements
<?php
/**
* Used to give the full hyperlink for dynamic items
*
* @param $type string - events/news
* @param $id int - ID of item for link
* @param $title string - item title for link to strip bad chars
**/
function seo_slug ( $type, $id, $title )
{
@AlexPashley
AlexPashley / get_alphabet.php
Last active September 5, 2021 07:35
PHP: Loops through ASCII characters to display alphabet, useful for alphabetic filtering links. eg: A | B | C | D ...
<?php
function get_alphabet()
{
$alphabet = '';
// Loop through ASCII characters until reaching 90
for ($i=65; $i<=90; $i++) {
// store the character
$letter = chr($i);
@AlexPashley
AlexPashley / replace.php
Last active January 25, 2023 23:40
PHP: function that will replace textual URLs with HTML links, this also works for email addresses.
<?php
function replace_links( $text )
{
$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);
$ret = ' ' . $text;
// Replace Links with http://
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\" rel=\"nofollow\">\\2</a>", $ret);
@AlexPashley
AlexPashley / dailyBackup.sh
Created July 3, 2013 20:30
SHELL: Simple Shell Script to create database backups
# Backup mysql databases into seperate files
USER="mysql-username"
PASSWORD="mysql-password"
OUTPUTDIR="/path/to/db/backup/dir"
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"
NOW=$(date +"%m-%d-%Y")
MSG="Just to let you know that a full backup of db has been made onto the VPS.Kind Regards"
# Remove previous backups
@AlexPashley
AlexPashley / pleskCommands.sh
Last active October 22, 2019 23:43
PLESK: Useful PLESK common commands
# USEFUL PLESK COMMANDS
# restart plesk
/etc/init.d/psa restart
# reload plesk configs (useful for vhost.conf)
/usr/local/psa/admin/sbin/websrvmng -a -v
# restart qmail
service qmail restart
@AlexPashley
AlexPashley / excludeFromList.sql
Created July 3, 2013 21:53
SQL: exclude "The", "An, "A" from results list
SELECT artist_name
FROM artists
ORDER BY TRIM(
LEADING "A "
FROM TRIM(
LEADING "An "
FROM TRIM(
LEADING "The "
FROM LOWER( artist_name ) ) ) )
LIMIT 30
@AlexPashley
AlexPashley / deleteDuplicateRows.sql
Created July 3, 2013 21:54
SQL: Delete duplicate rows from a mysql table
DELETE FROM table1
USING table1, table1 AS vtable
WHERE vtable.id > table1.id
AND table1.field_name = vtable.field_name
@AlexPashley
AlexPashley / roundNearestMultiple.php
Created July 3, 2013 21:56
PHP: Round to nearest multiple with PHP
/**
* Round up to the nearest multiple of your choice.
*
* @param int $number
* @param int $near
* @return int
*/
function get_nearest_multiple( $number, $near )
{
$nearest = round($number/$near)*$near;
@AlexPashley
AlexPashley / emailsFromCommandLine.sh
Created July 3, 2013 21:59
SHELL: Send emails from linux command line
# inline
mail -s "Subject of Message" [email protected] < /dev/null
# variables as parameters
$MSG="This is the body of the email address"
$SUBJET="Test Email Subject"
$ADDRESS="[email protected]"
echo $MSG | mail -s $SUBJECT $ADDRESS