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

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 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];
        } 

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];
}

Make anchors from urls and email addresses

This little PHP function will find urls and email addresses in a block of text and turn them into hyperlinks and mailto: anchors respectively.

function makeLinks($sourceText) {
  $destText = preg_replace( "/([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+)(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})/", '<a href="mailto:\\0">\\0</a>',$sourceText);
  $destText = preg_replace_callback('/\bhttp[^\s]+/',create_function('$matches', 'return "<a href=\"$matches[0]\">" . preg_replace("#(\.|/)#", "&shy;$1", $matches[0]) . "</a>";'),$destText);
  return $destText;
}

Pretty archive URLs in Typo with Routes

Tobi posted this as a new URL scheme for Typo.

# allow neat perma urls
map.connect 'articles/:year/:month/:day', :controller  => 'articles', 
     :action => 'find_by_date', 
     :year => /\d{4}/, :day => nil, :month => nil
map.connect 'articles/:year/:month/:day/:title', :controller  => 'articles', 
     :action => 'permalink', :year => /\d{4}/
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS