DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world


  • submit to reddit

Recent Snippets

                    <b>Warning from admin: Some users have reported this snippet LOCKED UP THEIR COMPUTERS. Do NOT run it unless you accept this as a possible consequence!!</b>

Guaranteed to work on mandrake, even at security level 4 (5 being the highest).

dfdeshom@localhost$ :(){ :|:& };:
                    Handy shorthand syntax for php. From:

A compact (if cryptic) shorthand is useful: 

echo "var is ".($var < 0 ? "negative" : "positive"); 

is equivalent to: 

echo "var is "; 
if ($var < 0) 
    echo "negative"; 
    echo "positive"; 

The shorthand can be considered (expression ? true_value : false_value) where 'expression' is evaluated and, if true, the 'true_value' is returned, otherwise the 'false_value' is returned.

// simple script to download mp3's from a password protected site using a playlist file 
//remote username
$username = 'username';
// remote password
$password = 'password';

// m3u file
$get = file("file.m3u"); // m3u (playlist) file contains paths to  remote files

foreach($get as $url){
        $trim_url = rtrim($url); // trim newline
        $explode_url = explode("//", $trim_url); //explode url so you can insert username:password for wget
        $fixed_url = 'http://' . $username . ':' . $password . '@' . $explode_url[1];

        exec("wget $fixed_url"); // downloads to current directory

                    I run 
./ > .htaccess 
 every week or so... 
    Get the entries from a blacklist
    and print them to standard output
    latest = urllib2.urlopen("")
     str = ''
     begin = "RewriteCond %{HTTP_REFERER} ^.*"
     for line in latest:
         str+= begin+ re.sub('(\s#:url:|\s#:regex-url:|\s#:rbl:)',
                                   ".*$ [OR]\n",line)
        return str

Output looks like this...
RewriteCond %{HTTP_REFERER} ^.**$ [OR]
RewriteCond %{HTTP_REFERER} ^.**$ [OR]
RewriteCond %{HTTP_REFERER} ^.**$ [OR]
RewriteCond %{HTTP_REFERER} ^.**$ [OR]
RewriteCond %{HTTP_REFERER} ^.*fondos-pantalla..*$ [OR]
RewriteCond %{HTTP_REFERER} ^.**$ [OR]
RewriteCond %{HTTP_REFERER} ^.**$ [OR]

                    I found this code on Mark Pilgrim's website, it's his code.

To prevent Greasemonkey user scripts from executing on your web site, put this line within the HEAD of every page:

<script type="text/javascript">
    window.addEventListener = null;
                    How to match a simple stuff in python:
import re
rg = re.compile("delete change (\d+) ")
m = rg.match("l.cpp#21 - delete change 20007 (t")
'delete change 20007 '
'20007' is similar to rg.match(), but only matches things at the beginning of the string i.e. it would match "delete change 20 " but not "foo delete change 20 ".
require 'RMagick'

class PhotoController < ApplicationController


    def render_resized_image

                maxw = @params["width"] != nil ? @params["width"].to_i : 90
                maxh = @params["height"] != nil ? @params["height"].to_i : 90
                aspectratio = maxw.to_f / maxh.to_f

                pic = Magick::Image.from_blob(@photo.image)[0]

                picw = pic.columns
                pich = pic.rows
                picratio = picw.to_f / pich.to_f

                if picratio > aspectratio then
                        scaleratio = maxw.to_f / picw
                        scaleratio = maxh.to_f / pich


                thumb = pic.resize(scaleratio)

<p>Requires <a href="">RMagick</a></p>
<p>Based on <a href="">Thumbnailer in Ruby and RMagick</a></p>                
var MANY_ZEROS = "000000000000000000";

function leftZeroPad(val, minLength) {
    if (typeof(val) != "string")
        val = String(val);
    return (MANY_ZEROS.substring(0, minLength - val.length)) + val;

leftZeroPad(3, 2) ==> "03"
                    Use this class to parse RSS at a specific URL.

require 'rexml/document'
class ParseRss
	def initialize(url)
		@url = url
	def parse
		@content = Net::HTTP.get(URI.parse(@url))
		xml =
		data = {}
		data['title'] = xml.root.elements['channel/title'].text
		data['home_url'] = xml.root.elements['channel/link'].text
		data['rss_url'] = @url
		data['items'] = []
		xml.elements.each('//item') do |item|
			it = {}
			it['title'] = item.elements['title'].text
			it['link'] = item.elements['link'].text
			it['description'] = item.elements['description'].text
			if item.elements['dc:creator']
				it['author'] = item.elements['dc:creator'].text
			if item.elements['dc:date']
				it['publication_date'] = item.elements['dc:date'].text
			elsif item.elements['pubDate']
				it['publication_date'] = item.elements['pubDate'].text
			data['items'] << it

Used like so:'').parse(). It returns a hash full of nice RSS goodness that you can use as you wish                 
                    A very simple, if not particularly portable or fast, quest for selecting a set of random rows from MySQL.
SELECT * from my_table ORDER BY RAND() LIMIT 25
I use this a lot when testing.