Count images from content

Aug 15th, 2009
function mbmCountImg($txt = ''){

preg_match_all('/<img[^>]+>/i',$txt, $result); //select img  tags

$img = array();
foreach( $result as $kk=>$v)
{
foreach( $result[$kk] as $k=>$vv)
{
preg_match_all('/(alt|title|src)=("[^"]*")/i',$vv, $img[$k]); //images into arrays
}
}

return count($img); //counting total arrays
}

Usage:

$content .= '';
$content .= '';
$content .= '';
$content .= '';

echo mbmCountImg($content);

Getting Youtube Video Information

Aug 15th, 2009
// function to parse a video 
function parseVideoEntry($entry) {
$obj= new stdClass;

// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
$obj->title = $media->group->title;
$obj->description = $media->group->description;

// get video player URL
$attrs = $media->group->player->attributes();
$obj->watchURL = $attrs['url'];

// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$obj->thumbnailURL = $attrs['url'];

// get  node for video length
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$obj->length = $attrs['seconds'];

// get  node for viewer statistics
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
$obj->viewCount = $attrs['viewCount'];

// get  node for video ratings
$gd = $entry->children('http://schemas.google.com/g/2005');
if ($gd->rating) {
$attrs = $gd->rating->attributes();
$obj->rating = $attrs['average'];
} else {
$obj->rating = 0;
}

// get  node for video comments
$gd = $entry->children('http://schemas.google.com/g/2005');
if ($gd->comments->feedLink) {
$attrs = $gd->comments->feedLink->attributes();
$obj->commentsURL = $attrs['href'];
$obj->commentsCount = $attrs['countHint'];
}

//Get the author
$obj->author = $entry->author->name;
$obj->authorURL = $entry->author->uri;

// get feed URL for video responses
$entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
$nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
2007#video.responses']“);
if (count($nodeset) > 0) {
$obj->responsesURL = $nodeset[0]['href'];
}

// get feed URL for related videos
$entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
$nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
2007#video.related']“);
if (count($nodeset) > 0) {
$obj->relatedURL = $nodeset[0]['href'];
}

// return object to caller
return $obj;
}

// get video ID from $_GET
//$vid = stripslashes($_GET['a']);
$vid = stripslashes('http://www.youtube.com/watch?v=N1Te_03drhk');
$string = $vid;
$url = parse_url($string);
parse_str($url['query']);

// set video data feed URL
print_r($url);
//$v = $_GET['v'];
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/'. $v;

// read feed into SimpleXML object
$entry = simplexml_load_file($feedURL);

// parse video entry
$video = parseVideoEntry($entry);

//These variables include the video information
$video_info['title'] = (stripslashes($video->title));
$video_info['description'] = (stripslashes($video->description));
$video_info['thumbnail'] = stripslashes($video->thumbnailURL);
$video_info['author'] = (stripslashes($video->author));

$video_info['watchURL'] = (stripslashes($video->watchURL));
$video_info['length'] = (stripslashes($video->length));
$video_info['viewCount'] = (stripslashes($video->viewCount));
$video_info['rating'] = (stripslashes($video->rating));
$video_info['commentsCount'] = (stripslashes($video->commentsCount));
$video_info['authorURL'] = 'http://www.youtube.com/'.$video_info['author'];//(stripslashes($video->authorURL));
// $video_info['responsesURL'] = (stripslashes($video->responsesURL));
// $video_info['relatedURL'] = (stripslashes($video->relatedURL));

$embed = '';

echo highlight_string($embed,1);
echo '

'; foreach($video_info as $k=>$v){ echo $k.': '.$v.''; }

Just copy paste the code to see the result.

File upload via FTP

Aug 15th, 2009
set_time_limit(0); //for huge files set time limit to zero that means unlimited.

if(isset($_POST['button'])){
$ftp_config['server'] = 'www.yahoo.com'; //ftp host
$ftp_config['username'] = 'batmunkh'; // ftp username
$ftp_config['password'] = 'mypass'; // ftp user password
$ftp_config['web_root'] = 'public_html'; //foldername from user home dir.

$fileElementName = 'userFile'; //file field name

$conn_id = ftp_connect($ftp_config['server']);
$ftp_login = ftp_login($conn_id,$ftp_config['username'],$ftp_config['password']);
$file_upload_limit_size = (1024*1024*1025*100);

if(!ftp_put($conn_id,$ftp_config['web_root'].'/'.$_FILES[$fileElementName]['name'],$_FILES[$fileElementName]['tmp_name'],FTP_BINARY)){
$result = " Error occurred.  ";
}else{
$result = " File has been uploaded. ";
}
echo $result;
}

HTML Form

File upload via FTP

Place all code in a same file

How to get FLV file duration

Aug 15th, 2009

I use this function to determine duration of a FLV file. It works pretty cool.

function mbmGetFLVDuration($file){
	// read file
	if (file_exists($file)){
		$handle = fopen($file, "r");
		$contents = fread($handle, filesize($file));
		fclose($handle);

		if (strlen($contents) > 3){
			if (substr($contents,0,3) == "FLV"){
				$taglen = hexdec(bin2hex(substr($contents,strlen($contents)-3)));
				if (strlen($contents) > $taglen){
					$duration = hexdec(bin2hex(substr($contents,strlen($contents)-$taglen,3)));
					return $duration;
				}
			}
		}
	}
	return false;
}

Paging function

Aug 15th, 2009
// paging function v2
function mbmNextPrev($url=NULL,$num_rows=0,$start=0,$per_page=10){
$total_pages = ceil($num_rows/$per_page);
$current_page = (($start/$per_page)+1);

$tmp_cccccc = 5; // how many page numbers will be printed

if(($current_page-$tmp_cccccc)>0){
$i_start = $current_page – $tmp_cccccc;
}else{
$i_start = 1;
}
if(($current_page+$tmp_cccccc)<$total_pages){
$end = $current_page + $tmp_cccccc;
}else{
$end = $total_pages;
}
if($current_page!=1){
$buf .= '';
$buf .= '';
$buf .= $lang['main']['paging_prev'].' ';
$buf .= '';
$buf .= '';
}
if($current_page>$tmp_cccccc && ($current_page-$tmp_cccccc)>1){
$buf .= '';
$buf .= '';
$buf .= 1;
$buf .= '';
$buf .= '';
if(($current_page-$tmp_cccccc)>2) $buf .= ' … ';
}
for($i=$i_start;$i<=$end;$i++){
if($i==$current_page){
$buf .= '';
$buf .= ''.$i.'';
}else{
$buf .= '';
$buf .= '';
$buf .= $i;
$buf .= '';
}
$buf .= '';
}
$buf = rtrim($buf,', ');

if($num_rows<$per_page){
return '';
}

if($total_pages != $current_page && $total_pages>$end){
$buf .= ' … ';
$buf .= '';
$buf .= '';
$buf .= $total_pages;
$buf .= '';
$buf .= '';
}

if($total_pages>$current_page){
$buf .= '';
$buf .= ''
.' '.$lang['main']['paging_next']
.'';
$buf .= '';
}

return '
'.$buf.'
'; }

Usage:

if(!isset($_GET['start'])) $start_i = 0;
else $start_i = $_GET['start'];
echo mbmNextPrev($PHP_SELF,$total_rows,$start_i,10);

Hello World

Aug 15th, 2009

It’s my blog’s Hello World. I have already started tutorials in Mongolian and i was planning to start a blog in English. Today is the day to start new blog. Here you will find some codes that can save time. I’ll try to make examples for each codes and functions or examples. Also i’ll try to prepare files to be downloadable.

Enjoy it.

First lesson is PHP Paging function.

Regards

BATMUNKH Moltov

P.S: try our search to find what you’re looking for.