Never been to DZone Snippets before?

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

About this user

Logan Koester http://logankoester.com

« Newer Snippets
Older Snippets »
Showing 1-10 of 12 total  RSS 

Create and lookup tinyurls with PHP

        function reverse_tinyurl($url){
            // Resolves a TinyURL.com encoded url to it's source.
            $url = explode('.com/', $url);
            $url = 'http://preview.tinyurl.com/'.$url[1];
            $preview = file_get_contents($url);
            preg_match('/redirecturl" href="(.*)">/', $preview, $matches);
            return $matches[1];
        }
        
        function tinyurl($url){
        // Shortens a url
            $html = file_get_contents("http://tinyurl.com/create.php?url=".$url);
            preg_match('/http:\/\/preview\.tinyurl\.com\/(.*)<\/b>/', $html, $matches);
            return "http://tinyurl.com/".$matches[1];
        } 

Display the number of characters in the name of each month

months = %w(January February March April May June July August September October November December)
months.each { |m| print  m, " (", m.length, ")\n" }

Rails route lets :id be a domain name

// Named route for an id that contains . characters, such as a domain name
// eg: http://myapp/domains/foobar.com

map.domain 'domains/:id', :controller => 'domains', :action => 'show', :requirements => { :id => /[a-zA-Z0-9\-\.]+/ }

Rename all *.gif files in a directory (prefix)

// My mind always seems to draw a blank when I want to do this sort of thing, so here's a concise little self-reminder

for file in $(echo *.gif); do mv ${file} prefix.${file}; done

Reverse TinyURL

// Resolves a TinyURL.com encoded url to it's source.
// Example: reverse_tinyurl('http://tinyurl.com/2ocfun') => "http://logankoester.com"

function reverse_tinyurl($url){
	$url = explode('.com/', $url);
	$url = 'http://preview.tinyurl.com/'.$url[1];
	$preview = file_get_contents($url);
	preg_match('/redirecturl" href="(.*)">/', $preview, $matches);
	return $matches[1];
}

Tidy Remote HTML (using a web service)

// Clean up some code using a web service. If you need to do this more quickly I suggest using a local tidy installation
// rather than my web service, but this is nice and easy. :)

function tidied($url) {
  /* Cleans up a page via Tidy, returning the cleaned up html as a string
   * By Logan Koester <logan@logankoester.com> 2007-06-28
   * Props to http://infohound.net/tidy */
  return file_get_contents("http://logankoester.com/tools/tidy.php?q=$url");
}

Generate a random string of letters

// Generates a random string of lowercase letters. Great for email verification codes ;)

Array.new(6) { (rand(122-97) + 97).chr }.join

Generate a random string of letters

// Generates a random string of lowercase letters. Great for email verification codes ;)

Array.new(6) { (rand(122-97) + 97).chr }.join

Generate a random string of letters

// Generates a random string of lowercase letters. Great for email verification codes ;)

Array.new(6) { (rand(122-97) + 97).chr }.join

Generate a random string of letters

Generates a random string of lowercase letters. Great for email verification codes ;)

Array.new(6) { (rand(122-97) + 97).chr }.join
« Newer Snippets
Older Snippets »
Showing 1-10 of 12 total  RSS