Snippets
Here's some of my tips, tweaks and tuts, ranging from PHP, Linux, MySQL and more!
Snippets / Plesk
Delete emails from email queue in PLESK
The GUI for PLESK's CMS is quite limited when you want to delete a large number of emails in one batch without crashing the website.
Through a linux command line you can filter and delete messages by either their email address, subject, from or to targets.
How to use qmHandle
qmHandle -h #################################################################### Available parameters: -a : try to send queued messages now (qmail must be running) -l : list message queues -L : list local message queue -R : list remote message queue -s : show some statistics -mN : display message number N -dN : delete message number N -fsender : delete message from sender -f're' : delete message from senders matching regular expression re -Stext : delete all messages that have/contain text as Subject -h're' : delete all messages with headers matching regular expression re (case insensitive) -b're' : delete all messages with body matching regular expression re (case insensitive) -H're' : delete all messages with headers matching regular expression re (case sensitive) -B're' : delete all messages with body matching regular expression re (case sensitive) -t're' : flag messages with recipients in regular expression 're' for earlier retry -D : delete all messages in the queue (local and remote) -V : print program version Additional (optional) parameters: -c : display colored output -N : list message numbers only (to be used either with -l, -L or -R)
Filter & Delete By Subject
qmHandle -S"Subject of Emails"
qmHandle Not installed?
There's a really helpful quick article about how to get this program working on your linux server - Support Facility Blog
13 years ago / Read More
Snippets / Linux
Turn on syntax highlighting VIM
If you want to turn on syntax highlighting for vim editor on Ubuntu such edit the main config file here
# open vim settings vim /etc/vim/vimrcThen uncomment " syntax on " around line 20
# leave setting syntax on
If you just wanted to turn it on just while you were editing a file (not a permanent solution)
:syntax on
Then press "Enter"
13 years ago / Read More
Snippets / Linux
Remember Alias binds - Linux
Binding Aliases in linux is a very useful way shorten your workload and the amount you type!
In order to save your alias binds you need to save them in the .profile file which should be in the login root directory.
An Example
# open file to edit vim .profile # add alias bind alias zf=/path/to/shell/script.sh
Remember to save the file and logout and login again.
13 years ago / Read More
Snippets / PHP
Strip non-alphanumerical characters with PHP
In the past, when I first started using PHP in order to filter out bad characters for file uploads, seo friendly URL names and other things I created an array of bad characters.
This is obviously not an ideal situation as the amount of characters that people or scripts can inject is ridiculously high, so adding a new element in the array for this becomes unpractical.
Old Way
// manually enter each bad character // Text to clean $text = "This is @ 'sentence' with characters in it!!! "; // wrong wrong wrong!! $bad_chars = array('/', " ", "!", "%", "&", "]", "[", "*", "<", ">",";", "`", "¬", "$", "£", "^"); // Start Cleaning $text = str_replace($bad_chars,'',$text); // print text echo $text;
A more powerful and easy way to do this would be with regular expressions and filter just alpha-numerical characters. The example below shows how to do it with and without spaces.
- 0-9
- A-Z
Improved Way
// Text to clean $text = "This is @ '''sentence''' _without_ characters in it!!! "; // Start Cleaning - Letters Only $text = ereg_replace("[^A-Za-z]", "", $text); // Start Cleaning - Without Spaces $text = preg_replace("/[^A-Za-z0-9]/", "", $text ); // Start Cleaning - With Spaces $text = preg_replace("/[^a-zA-Z0-9s]/", "", $text ); // Show results echo $text; // This is sentence without characters in it
13 years ago / Read More
Snippets / MySQL
Filter results by todays date - MySQL
MySQL is a very powerful open source database and there was no suprise when I found there was a way to retrieve records from a database that expired at @ 11.59 of the "end_date" field. You can alternatively do this via php date() function as well.
The Solution
The Query
SELECT id, title FROM meetings WHERE start_date < NOW() AND end_date > DATE(NOW()) ORDER BY id DESC
13 years ago / Read More
Snippets / PHP
Replace URLs with links - PHP
- Technologies: PHP, HTML
- Difficulty: Easy
- Time: 5 minutes
Where I work, we develop many websites that have blogs and news sections. Often we allow users to add their own HTML snippets into the content of the articles in order to enhance the look and feel of the story.
The Problem
We often find that different users have different levels of experience when it comes to "the web". Some people tend to enter invalid HTML which can compromise the design, layout, functionality and many other things.
Soooo with trying to keep all HTML snippets at a minium I wanted to find a way in which users can add links which adding any HTML at all.
So for example:
www.google.co.uk & http://images.google.co.uk
Would become the following AUTOMATICALLY:
www.google.co.uk & http://images.google.co.uk
The Solution
The Code
function replace_links( $text ) { $text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text); $ret = ' ' . $text; $ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1\\2", $ret); $ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1\\2", $ret); $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1\\2@\\3", $ret); $ret = substr($ret, 1); return $ret; }
13 years ago / Read More
Snippets / PHP
Loop through the Alphabet with PHP
- Difficulty: Easy
- Snippet Length: 1 minute
- Technologies: PHP, HTML
I have several times wanted to provide a client with a facility to filter data by an alphabetic character and have done so by manually typing each letter of the alphabet with a hard link for example.
Old Way
etc...
Short & Sweet way
I realised that I can use PHP's inbuilt chr function to echo out the relevant ASCII character, the capitalised alphabetic sequence starts from 65 so by just incrementing until 90 will give us all of our desired chars.
Let's try it out
for ($i=65; $i<=90; $i++) { echo chr($i); }
Adding a little markup
// Loop through ASCII characters until reaching 90 for ($i=65; $i<=90; $i++) { // store the character $letter = chr($i); // build alphabetical link $alphabet .= '<a title="filter results by letter '.$letter.'" href="/business/'.$letter.'"> '; $alphabet .= $letter; $alphabet .= '</a> | '; } // print links echo $alphabet;
13 years ago / Read More
Snippets / jQuery
Go to top of page with jQuery
http://www.nomadjourney.com/2009/09/go-to-top-of-page-using-jquery/
13 years ago / Read More