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

                    You can use curl on the command line to do a POST to an endpoint. 

echo '<doc><item>Some content.</item></doc>' | curl -X POST -H 'Content-type: text/xml' -d @- http://example.com/restapi

This is handy for adding web services to applications that do not do web services but can do command lines--FileMaker for example.                
                    This will replace your H1 tag with a 100px by 100px logo.
Users with CSS disabled will see whatever is in 
<h1>Company Inc.</h1>
users with CSS will see the Company Inc. logo
to use this, add  this to your stylesheet:
h1 {
	text-indent: -100em;
	overflow: hidden;
	background: url(logo.png) no-repeat;
	height: 100px;
	width: 100px;
	}
                
                    module MetaBottle
  def self.make(x)
    Class.new do
      define_method(:bottle) do
        case x
        when 0 then "no more bottles"
        when 1 then "1 bottle"
        else        "#{x} bottles"
        end + " of beer"
      end
      define_method(:bottle_minus_one) do
        case x - 1
        when 0 then "no more bottles"
        when 1 then "1 bottle"
        else        "#{x} bottles"
        end + " of beer"
      end
      define_method(:to_s) do
        return <<T
#{bottle} on the wall, #{bottle},
take one down, pass it around,
#{bottle_minus_one} on the wall.\n
T
      end
    end
  end
end

(1..99).map { |x| MetaBottle.make(x) }.reverse_each do |bottle_class|
  bottle = bottle_class.new
  puts bottle
end
                
                    
javascript:q=document.getSelection();for(i=0;i<frames.length;i++){q=frames[i].document.getSelection();if(q)break;}if(!q)void(q=prompt('Statistically Improbable Phrase:',''));if(q)location.href='http://www.amazon.com/gp/phrase/'+escape(q)+'/ref=sip_bod_0/103-6410487-8187016'
                
                    require 'enumerator'

ary = [ 1, 2, 3, 4 ]

# iterate over two elements at a time
ary.each_slice(2) { |a,b| p [a, b] }

# iterate over every pair of consecutive pair of numbers
ary.each_cons(2) { |a, b| p [a, b] }

# An Enumerable::Enumerator object can be created as well,
# that mixes in Enumerable, for further processing:
ary.enum_for(:each_cons, 2).map { |a,b| a + b  } # => [3, 5, 7]
                
                    
function get_os_($user_agent)
{
	$oses = array (
		'Windows 3.11' => 'Win16',
		'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
		'Windows 98' => '(Windows 98)|(Win98)',
		'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
		'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
		'Windows 2003' => '(Windows NT 5.2)',
		'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
		'Windows ME' => 'Windows ME',
		'Open BSD'=>'OpenBSD',
		'Sun OS'=>'SunOS',
		'Linux'=>'(Linux)|(X11)',
		'Macintosh'=>'(Mac_PowerPC)|(Macintosh)',
		'QNX'=>'QNX',
		'BeOS'=>'BeOS',
		'OS/2'=>'OS/2',
		'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp/cat)|(msnbot)|(ia_archiver)'
	);

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

For using:
print get_os_($_SERVER['HTTP_USER_AGENT']);
                
                    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)