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

Snippets

  • submit to reddit

Recent Snippets

                    
mysql> CREATE TABLE articles (
    ->   id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
    ->   title VARCHAR(200),
    ->   body TEXT,
    ->   FULLTEXT (title,body)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO articles (title,body) VALUES
    -> ('MySQL Tutorial','DBMS stands for DataBase ...'),
    -> ('How To Use MySQL Well','After you went through a ...'),
    -> ('Optimizing MySQL','In this tutorial we will show ...'),
    -> ('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
    -> ('MySQL vs. YourSQL','In the following database comparison ...'),
    -> ('MySQL Security','When configured properly, MySQL ...');
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM articles
    -> WHERE MATCH (title,body) AGAINST ('database');
+----+-------------------+------------------------------------------+
| id | title             | body                                     |
+----+-------------------+------------------------------------------+
|  5 | MySQL vs. YourSQL | In the following database comparison ... |
|  1 | MySQL Tutorial    | DBMS stands for DataBase ...             |
+----+-------------------+------------------------------------------+
2 rows in set (0.00 sec)

More information here.
http://dev.mysql.com/doc/mysql/en/fulltext-search.html                
                    No need to use temp var.
a, b = b, a
                
                    I am really impressed by this.

import messaging
messaging.sms_send(number, text)
                
                    Reading remote data is easy in python.

from urllib import *
url = 'http://example.com/index.html'
html = urlretrieve(url).read()
                
                    I am trying to use the site.
Here's my first program.

print "hello world"
                
                    this is a very basic script for seperating out values and units.  this is helpful for changing values such as style.top or border.

if element.style.top => '15px'
splitUnit( element.style.top ) => [15,'px']

function splitUnit(e)
/*
*	takes a string and seperates 'px or 'em' from it
*
*	could be streamlined to simply seperate the last two characters, but that could cause errors if used sloppily
*/
{
	if( e && ( length = e.search( /px/ ) ) ) {var eUnit = 'px'; var eValue = e.substr( 0, length );}
	else if( e && ( length = e.search( /em/ ) ) ) {var eUnit = 'em'; var eValue = e.substr( 0, length );}
	else return false;
	
	return new Array( eValue, eUnit );
}
                
                    An equivalent of the other find-replace, except it's a one-liner that generates no temp files, and is more flexible:

perl -pi -e 's/find/replace/g' *.txt

Or, to change matching files in a hierarchy:

find . -name '*.txt' |xargs perl -pi -e 's/find/replace/g'
                
                    Assuming you have mod_rewrite and AllowOverride activated, throw this in the .htaccess file for the directory you don't want to be Slashdotted:

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^.*slashdot.*$
RewriteRule .* - [F]

This all assumes the editors actually check the links before posting the story, but this should reasonably mean no-one on Slashdot can link to you, even if it's a bit over the top.                
                    This will output the data from a URL to a file and then display the data in that file.

wget -q -O file "url"; cat file
                
                    a function to convert common used bbcode tags to html... as usually used in message boards and some blog software.

it handles img, email, link, b, u and i tags...

for example
[b]test[/b]
[url=http://blah.com]blah[/url]
will become:
<strong>test</strong>
<a href="http://blah.com" target="_blank">blah</a>

function ubbcode($posting) {
	$posting=eregi_replace("\[img\]([^\[]+)\[/img\]","<img src=\"\\1\" border=\"0\">",$posting);
	$posting=eregi_replace("\[email\]([^\[]+)\[/email\]","<a href=\"mailto:\\1\">\\1</a>",$posting);
	$posting=eregi_replace("\[email=([^\[]+)\]([^\[]+)\[/email\]","<a href=\"mailto:\\1\">\\2</a>",$posting);
	$posting=eregi_replace("\[url=([^\[]+)\]([^\[]+)\[/url\]","<a href=\"\\1\" target=\"_blank\">\\2</a>",$posting);
	$posting=eregi_replace("\[url\]([^\[]+)\[/url\]","<a href=\"\\1\" target=\"_blank\">\\1</a>",$posting);
    $posting=eregi_replace("\[b\]","<strong>",$posting);
    $posting=eregi_replace("\[/b\]","</strong>",$posting);
    $posting=eregi_replace("\[u\]","<u>",$posting);
    $posting=eregi_replace("\[/u\]","</u>",$posting);
    $posting=eregi_replace("\[i\]","<em>",$posting);
    $posting=eregi_replace("\[/i\]","</em>",$posting);

    return $posting;
}