The Code Cookbook - Ratinghttp://code-cookbook.com/The Code Cookbook - Freshly served code snippets Colored Output in Bash Function http://code-cookbook.com/entry/62/colored-output-in-bash-function Simple bash function for adding color to output.

#!/bin/bash
#
# Example usage:
# echo ${RedF}This text will be red!${Reset}
# echo ${BlueF}{$BoldOn}This will be blue and bold!${BoldOff} - and this is just blue!${Reset}
# echo ${RedB}${BlackF}This has a red background and black font!${Reset}and everything after the reset is normal text!

Colors() {
  Escape="\033";

  BlackF="${Escape}[30m";   RedF="${Escape}[31m";   GreenF="${Escape}[32m";
  YellowF="${Escape}[33m";  BlueF="${Escape}[34m";    Purplef="${Escape}[35m";
  CyanF="${Escape}[36m";    WhiteF="${Escape}[37m";

  BlackB="${Escape}[40m";     RedB="${Escape}[41m";     GreenB="${Escape}[42m";
  YellowB="${Escape}[43m";    BlueB="${Escape}[44m";    PurpleB="${Escape}[45m";
  CyanB="${Escape}[46m";      WhiteB="${Escape}[47m";

  BoldOn="${Escape}[1m";      BoldOff="${Escape}[22m";
  ItalicsOn="${Escape}[3m";   ItalicsOff="${Escape}[23m";
  UnderlineOn="${Escape}[4m";     UnderlineOff="${Escape}[24m";
  BlinkOn="${Escape}[5m";   BlinkOff="${Escape}[25m";
  InvertOn="${Escape}[7m";  InvertOff="${Escape}[27m";

  Reset="${Escape}[0m";
}
2009-02-24 22:45:56 http://code-cookbook.com/entry/62/colored-output-in-bash-function
PHP Limit File Download Speed http://code-cookbook.com/entry/57/php-limit-file-download-speed This snippet shows you how to limit the download rate of a file download.

// local file that should be send to the client
$local_file = 'test-file.zip';
// filename that the user gets as default
$download_file = 'your-download-name.zip';
 
// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5; 
if(file_exists($local_file) && is_file($local_file)) {
    // send headers
    header('Cache-control: private');
    header('Content-Type: application/octet-stream'); 
    header('Content-Length: '.filesize($local_file));
    header('Content-Disposition: filename='.$download_file);
 
    // flush content
    flush();    
    // open file stream
    $file = fopen($local_file, "r");    
    while(!feof($file)) {
 
        // send the current file part to the browser
        print fread($file, round($download_rate * 1024));    
 
        // flush the content to the browser
        flush();
 
        // sleep one second
        sleep(1);    
    }    
 
    // close file stream
    fclose($file);}
else {
    die('Error: The file '.$local_file.' does not exist!');
}
2009-02-23 06:51:16 http://code-cookbook.com/entry/57/php-limit-file-download-speed
CSS Min Height IE http://code-cookbook.com/entry/46/CSS-Min-Height-IE This works in IE, Opera, Firefox, Netscape, Safari. All browsers that support min-height are given a min-height of 8em with height:auto which will expand the container to fit the text. Internet Explorer will ignore min-height and is just given a height of 8em. The IE bug automatically expands the container to fit the extra text.

/* for understanding browsers */
.container {
width:20em;
padding:0.5em;
border:1px solid #000;
min-height:8em; 
height:auto;
}
/* for Internet Explorer */
/*\*/
* html .container {
height: 8em;
}
/**/
2009-02-19 08:23:24 http://code-cookbook.com/entry/46/CSS-Min-Height-IE
Bash Script to Post to Twitter http://code-cookbook.com/entry/75/bash-script-to-post-to-twitter Here is a quick/easy way to post to twitter from the command line interface. Put the script in /usr/local/bin/ Then it is as easy as: user@river:~$ twitter Your Message Here

#!/bin/bash

# Login information.
USERNAME="email address"
PASSWORD="password"
URL=http://twitter.com/statuses/update.xml

# Check message length.
if [ ! -n "$1" ]; then
        echo "Message not long enough"
        exit
fi

# Check message length again.
message="$@"
maxlen=140;
len=`echo ${#message}`

if [ $len -gt $maxlen ]; then
        echo "Your message was longer than 140 characters...";
fi

# Post to Twitter.
result=`curl -u $USERNAME:$PASSWORD -d status="$message" $URL`
2009-03-06 17:03:51 http://code-cookbook.com/entry/75/bash-script-to-post-to-twitter
High Quality CSS Thumbnails in IE7 http://code-cookbook.com/entry/76/high-quality-css-thumbnails-in-ie7 IE7 supports a custom bicubic resampling mode for images. This produces a much higher image quality for resized images.

/* HTML:
<img src="pic.jpg" alt="This image is really 500x500 big" class="thumb" width="50" height="50" />
*/

img.thumb { -ms-interpolation-mode: bicubic; }
2009-03-13 10:00:03 http://code-cookbook.com/entry/76/high-quality-css-thumbnails-in-ie7
PHP Regex Valid Email Check http://code-cookbook.com/entry/51/php-regex-valid-email-check If you want a PHP script to verify an email address then use this quick and simple PHP regular expression for email validation. This is also case-insensitive, so it will treat all characters as lower case. It is a really easy way to check the syntax and format of an email address.

<?php
$email = "someone@example.com";
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
  echo "Valid email address.";
}
else {
  echo "Invalid email address.";
}
?>
2009-02-23 06:21:11 http://code-cookbook.com/entry/51/php-regex-valid-email-check
Htaccess Clean URL's http://code-cookbook.com/entry/48/htaccess-clean-urls A basic example of how to use .htaccess to create clean urls on your website.

#.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?page=$1 [L]
</IfModule>

# http://www.example.com/contact -> 
# http://www.example.com/index.php?page=contact
#
# <?php
# // Get the data from the url
# $url_segments = explode("/", $HTTP_SERVER_VARS['PATH_INFO']);
# ?>
2009-02-23 02:56:40 http://code-cookbook.com/entry/48/htaccess-clean-urls
HTML Conditional Comments http://code-cookbook.com/entry/45/HTML-Conditional-Comments Conditional comments only work in Explorer on Windows, and are thus excellently suited to give special instructions meant only for Explorer on Windows. They are supported from Explorer 5 onwards, and it is even possible to distinguish between 5.0, 5.5 and 6.0.

<p><!--[if IE]>
According to the conditional comment this is Internet Explorer<br />
<![endif]-->
<!--[if IE 5]>
According to the conditional comment this is Internet Explorer 5<br />
<![endif]-->
<!--[if IE 5.0]>
According to the conditional comment this is Internet Explorer 5.0<br />
<![endif]-->
<!--[if IE 5.5]>
According to the conditional comment this is Internet Explorer 5.5<br />
<![endif]-->
<!--[if IE 6]>
According to the conditional comment this is Internet Explorer 6<br />
<![endif]-->
<!--[if IE 7]>
According to the conditional comment this is Internet Explorer 7<br />
<![endif]-->
<!--[if gte IE 5]>
According to the conditional comment this is Internet Explorer 5 and up<br />
<![endif]-->
<!--[if lt IE 6]>
According to the conditional comment this is Internet Explorer lower than 6<br />
<![endif]-->
<!--[if lte IE 5.5]>
According to the conditional comment this is Internet Explorer lower or equal to 5.5<br />
<![endif]-->
<!--[if gt IE 6]>
According to the conditional comment this is Internet Explorer greater than 6<br />
<![endif]-->
</p>
2009-02-19 08:15:03 http://code-cookbook.com/entry/45/HTML-Conditional-Comments
HTML Meta Tags http://code-cookbook.com/entry/68/html-meta-tags META tags should be placed in the head of the HTML document, between the &lt;HEAD&gt; and &lt;/HEAD&gt; tags (especially important in documents using FRAMES).

<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
<META HTTP-EQUIV="Content-Language" CONTENT="en-GB">
<META HTTP-EQUIV="Refresh" CONTENT="3;URL=http://www.some.org/some.html">
<META HTTP-EQUIV="Window-target" CONTENT="_top">
<META HTTP-EQUIV="Ext-cache" CONTENT="name=/some/path/index.db; instructions=User Instructions">
<META HTTP-EQUIV="Set-Cookie" CONTENT="cookievalue=xxx;expires=Friday, 31-Dec-99 23:59:59 GMT; path=/">
<META NAME="ROBOTS" CONTENT="NOINDEX,FOLLOW">
<META NAME="description" CONTENT="Citrus fruit wholesaler.">
<META NAME="keywords" CONTENT="oranges, lemons, limes">

2009-02-27 01:29:40 http://code-cookbook.com/entry/68/html-meta-tags
Substitue a Physical Path as a Virtual Drive http://code-cookbook.com/entry/63/substitue-a-physical-path-as-a-virtual-drive The example below would make a virtual B:\ drive which would point to your documents folder.

subst b: "C:\Documents and Settings\user\My Documents"
2009-02-25 02:21:13 http://code-cookbook.com/entry/63/substitue-a-physical-path-as-a-virtual-drive
PHP Automatically Changing Copyright Year http://code-cookbook.com/entry/64/php-automatically-changing-copyright-year Add this to your website footer so you don't have to update it every year.

Copyright &copy; <?php echo date('Y'); ?> Example.com.
2009-02-25 05:35:20 http://code-cookbook.com/entry/64/php-automatically-changing-copyright-year
Wordpress "Tweet This" Link http://code-cookbook.com/entry/65/wordpress-tweet-this-link

<a href="http://twitter.com/home?status=I just read <?php the_permalink(); ?>" title="Send this page to Twitter!" target="_blank">Tweet This!</a>
2009-02-25 05:37:15 http://code-cookbook.com/entry/65/wordpress-tweet-this-link
Wordpress Separate Trackbacks / Pingbacks from Comments http://code-cookbook.com/entry/66/wordpress-separate-trackbacks-pingbacks-from-comments This is a neat little Wordpress trick to separate real comments from trackbacks and pingbacks.

<?php if ( $comments ) : ?>
<?php foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type == 'comment') { ?>
 
<!-- It's a comment -->
<!-- Comment content goes here -->
 
<?php } else { $trackback = true; }?> 
<?php endforeach; ?>
<?php if ($trackback == true) { ?>
 
<!-- It's a trackback -->
  <ol id="trackbacks-ol">
	  <?php foreach ($comments as $comment) : ?>
	  <?php $comment_type = get_comment_type(); ?>
	  <?php if($comment_type != 'comment') { ?>
	  <li>
		<?php comment_author_link() ?>
	</li>
	  <?php } ?>
	  <?php endforeach; ?>
  </ol>
 
<?php } ?>
<?php else : ?>
<?php endif; ?>
2009-02-25 05:41:08 http://code-cookbook.com/entry/66/wordpress-separate-trackbacks-pingbacks-from-comments
hCard Microformat http://code-cookbook.com/entry/55/hcard-microformat hCard is a simple, open, distributed format for representing people, companies, organizations, and places, using a 1:1 representation of vCard (RFC2426) properties and values in semantic HTML or XHTML.

<div id="contact" class="vcard">
   <h2>Contact Me</h2>
   <h3 class="fn">Jane Doe</h3>
   <p>You can contact me via email to 
    <a class="email" href="mailto:jane@example.com">jane@example.com</a>, 
    or reach me at the following address:</p>
   <div class="adr">
     <div class="street-address">255 Some Street</div>
     <div class="locality">Some Town</div>
     <div class="region">Some Place</div>
   </div>
</div>
2009-02-23 06:43:11 http://code-cookbook.com/entry/55/hcard-microformat
CSS Image Preloader http://code-cookbook.com/entry/56/css-image-preloader A low-tech but useful technique that uses only CSS. After placing the css in your stylesheet, insert this just below the body tag of your page. Whenever the images are referenced throughout your pages they will now be loaded from cache.

#preloadedImages {
       width: 0px;
       height: 0px;
       display: inline;
       background-image: url(path/to/image1.png);
       background-image: url(path/to/image2.png);
       background-image: url(path/to/image3.png);
       background-image: url(path/to/image4.png);
       background-image: url();
}
2009-02-23 06:46:40 http://code-cookbook.com/entry/56/css-image-preloader