Archive for the ‘PHP’ Category.

How to redirect your RSS Feed to Feedburner

This is a really simple yet handly tip to redirect your feed to Feedburner.

Edit, or create an .htaccess file in your htdocs root, and add this lines:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} !FeedBurner
RewriteRule ^rss/$ http://feeds.feedburner.com/yourURL [R,L]
</IfModule>

Now any request to yoursite.com/feed will be redirected to the custom feedburner feed ;)

Be aware that only works in apache.

Youtube brackets

I’ve been using youtube bracket for wp for long time.

Based on that Idea I isolated it to make it work at any installation.

This script allows you to write anywhere in your content this [youtube=http://www.youtube.com/watch?v=x89xAXHd2l8] and will return a flash player with the video embed.

Function that embeds the flash in the content:

$content=mediaPostDesc($content);//usage
 
function mediaPostDesc($the_content){//from a description add the media
//using http://www.robertbuzink.nl/journal/2006/11/23/youtube-brackets-wordpress-plugin/
    if (VIDEO){
        $stag = "[youtube=http://www.youtube.com/watch?v=";
        $etag = "]";
        $spos = strpos($the_content, $stag);
        if ($spos !== false){
            $epos = strpos($the_content, $etag, $spos);
            $spose = $spos + strlen($stag);
            $slen = $epos - $spose;
            $file  = substr($the_content, $spose, $slen);    
			//youtube
            $tags = '<object width="425" height="350">
                    <param name="movie" value="'.$file.'"></param>
                    <param name="wmode" value="transparent" ></param>
                    <embed src="http://www.youtube.com/v/'. $file.'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed>
                    </object>';    
            $new_content = substr($the_content,0,$spos);
            $new_content .= $tags;
            $new_content .= substr($the_content,($epos+1));
 
            if ($epos+1 < strlen($the_content)) {//reciproco
                $new_content = mediaPostDesc($new_content);
            }
            return $new_content;
        }
        else return $the_content;
    }
    else return $the_content;
}

Continue reading ‘Youtube brackets’ »

Compress HTML before sending to the browser

Hany function ob_gzhandler that help facilitate sending gz-encoded data to web browsers that support compressed web pages.

This is the way I do it, checking the extension it’s loaded and that it’s possible to start the encoding:

if (extension_loaded('zlib')) {//check extension is loaded
    if(!ob_start("ob_gzhandler")) ob_start();//start HTML compression, if not normal buffer input mode
}

Detect file extension in PHP

Super easy trick to find a file extension:

$file="some_file.jpg";
$ext=end(explode(".", $file);

Other ways:

Substring:

$ext = substr($file, strrpos($file, '.') + 1);
//or:
$ext= substr(strrchr($file, "."), 1 );

Using path info:

$info = pathinfo($file);
$ext=$info['extenstion'];

Function to check if visitor is a bot

This function will check whether the visitor is a search engine robot

function is_bot(){
	$botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi",
	"looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",
	"Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",
	"crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",
	"msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",
	"Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",
	"Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot",
	"Butterfly","Twitturls","Me.dium","Twiceler");
 
	foreach($botlist as $bot){
		if(strpos($_SERVER['HTTP_USER_AGENT'],$bot)!==false)
		return true;	// Is a bot
	}
 
	return false;	// Not a bot
}

There’s any other better way?

Follow me