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

                    This function determines visitor browser.

function get_browser_($user_agent)
{
	$browsers = array(
		'Opera' => 'Opera',
		'Mozilla Firefox'=> '(Firebird)|(Firefox)',
		'Galeon' => 'Galeon',
		'Mozilla'=>'Gecko',
		'MyIE'=>'MyIE',
		'Lynx' => 'Lynx',
		'Netscape' => '(Mozilla/4\.75)|(Netscape6)|(Mozilla/4\.08)|(Mozilla/4\.5)|(Mozilla/4\.6)|(Mozilla/4\.79)',
		'Konqueror'=>'Konqueror',
		'SearchBot' => '(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp/cat)|(msnbot)|(ia_archiver)',
		'Internet Explorer 6' => '(MSIE 6\.[0-9]+)',
		'Internet Explorer 5' => '(MSIE 5\.[0-9]+)',
		'Internet Explorer 4' => '(MSIE 4\.[0-9]+)',
	);

	foreach($browsers as $browser=>$pattern)
	{
		if (eregi($pattern, $user_agent))
			return $browser;
	}
	return 'Unknown';
}

For using:
echo get_browser_($_SERVER['HTTP_USER_AGENT']);
                
                    was getting this error on certain files until I added this to ~/.bash_profile :
export LC_CTYPE="en_US.UTF-8"
export LANG="en_US.UTF-8"
                
                    Include this to convert those ampersands and angle brackets to proper entities. It can help with XML too but remember there are lots of other entities that need converting. 

use HTML::Entities;

...

$html = encode_entities($string, '<>&"');
                
                    ([12][0-9][0-9](0[1-9]|1[0-2])(0[1-9]|[13456789][0-9]|2[023456789AB])[0-9][0-9][0-9][0-9][0-9][0-9])([0-9][0-9])

cf http://u-blog.net/dda/note/11 pour les explications
(et reste à valider la clé de contrôle)                
                    The "cvs status" command is practically useless, given the amount of stuff it spews out. So I do the following to find out the status of my local working copy. This shows all updated, added, or deleted files, as well as those CVS know nothing about (and which perhaps should be added):

cvs status 2>&1 | egrep "(^\? |Status: )" | grep -v Up-to-date
                
                    A neat trick in iTunes 4.5 that changes the little arrows next to each artist/album/title to search your library for that string instead of the iTunes Music Store.

defaults write com.apple.iTunes invertStoreLinks -bool YES
                
                    Sometimes you want to log generated SQL selectively, so popping -DEOAdaptorDebugEnabled=true on the command line of your app is verbose overkill. Here's how you can selectively enable and disable SQL logging:

NSLog.allowDebugLoggingForGroups(0x10000L);
// Do something that roundtrips the DB, like editingContext.saveChanges().
NSLog.refuseDebugLoggingForGroups(0x10000L);
                
                    
(new DOMConfigurator()).doConfigure(resourceManager().inputStreamForResourceNamed("log4j.xml", "app", null), LogManager.getLoggerRepository());
                
                    
NSLog.debug.setAllowedDebugLevel(NSLog.DebugLevelDetailed);
NSLog.allowDebugLoggingForGroups(NSLog.DebugGroupRules);
                
                    
# -*- coding: utf-8 -*-

Add this line at the beginning of your code. Python won't complain anymore about non-ascii characters in your code.