Hướng dẫn dùng incroments trong PHP

' . htmlspecialchars[ stripslashes[ $tag ] ] . ''; } return join[ "\n", $cloudTags ] . "\n"; } /************************** **** Sample usage ***/ $arr = Array['Actionscript' => 35, 'Adobe' => 22, 'Array' => 44, 'Background' => 43, 'Blur' => 18, 'Canvas' => 33, 'Class' => 15, 'Color Palette' => 11, 'Crop' => 42, 'Delimiter' => 13, 'Depth' => 34, 'Design' => 8, 'Encode' => 12, 'Encryption' => 30, 'Extract' => 28, 'Filters' => 42]; echo getCloud[$arr, 12, 36];

Chèn Avater với gravatar

Với sự phát triển chóng mặt của WordPress, Gravatar trở nên phổ biến hơn. Nó khá dễ dàng để tích hợp hình ảnh avatar cho website bằng cách sử dụng API của dịch vụ này.

/******************
*@email - Email address to show gravatar for
*@size - size of gravatar
*@default - URL of default gravatar to use
*@rating - rating of Gravatar[G, PG, R, X]
*/
function show_gravatar[$email, $size, $default, $rating]
{
	echo '';
}

File nén ZIP

Để tạo tạo file nén bạn lấy đoạn code sau:

/* creates a compressed zip file */
function create_zip[$files = array[],$destination = '',$overwrite = false] {
	//if the zip file already exists and overwrite is false, return false
	if[file_exists[$destination] && !$overwrite] { return false; }
	//vars
	$valid_files = array[];
	//if files were passed in...
	if[is_array[$files]] {
		//cycle through each file
		foreach[$files as $file] {
			//make sure the file exists
			if[file_exists[$file]] {
				$valid_files[] = $file;
			}
		}
	}
	//if we have good files...
	if[count[$valid_files]] {
		//create the archive
		$zip = new ZipArchive[];
		if[$zip->open[$destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE] !== true] {
			return false;
		}
		//add the files
		foreach[$valid_files as $file] {
			$zip->addFile[$file,$file];
		}
		//debug
		//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
		
		//close the zip -- done!
		$zip->close[];
		
		//check to make sure the file exists
		return file_exists[$destination];
	}
	else
	{
		return false;
	}
}
/***** Example Usage ***/
$files=array['file1.jpg', 'file2.jpg', 'file3.gif'];
create_zip[$files, 'myzipfile.zip', true];

Bạn cũng có thể sử dụng PHP để giải nén file zip.

/**********************
*@file - path to zip file
*@destination - destination directory for unzipped files
*/
function unzip_file[$file, $destination]{
	// create object
	$zip = new ZipArchive[] ;
	// open archive
	if [$zip->open[$file] !== TRUE] {
		die [’Could not open archive’];
	}
	// extract contents to destination directory
	$zip->extractTo[$destination];
	// close archive
	$zip->close[];
	echo 'Archive extracted to directory';
}

Tạo hyperlinks từ chuỗi chứa URL

Hàm này sẽ chuyển URL và địa chỉ email chứa trong chuỗi thành thẻ liên kết HTML có thể nhấn được trên website.

function makeClickableLinks[$text] {
 $text = eregi_replace['[[[f|ht]{1}tp://][[email protected]:%_+.~#?&//=]+]',
 '\1', $text];
 $text = eregi_replace['[[[:space:][][{}]][www.[[email protected]:%_+.~#?&//=]+]',
 '\1\2', $text];
 $text = eregi_replace['[[_.0-9a-z-][email protected][[0-9a-z][0-9a-z-]+.]+[a-z]{2,3}]',
 '\1', $text];

return $text;
}

Thay đổi kích thước ảnh

Từ kích thước ảnh gốc bạn sẽ muốn tạo ra nhiều kích thước khác nhau của ảnh để hiển thị phù hợp trên website. Đoạn code dưới đây là cách để sinh ra kích thước thumbnail.

/**********************
*@filename - path to the image
*@tmpname - temporary path to thumbnail
*@xmax - max width
*@ymax - max height
*/
function resize_image[$filename, $tmpname, $xmax, $ymax]
{
	$ext = explode[".", $filename];
	$ext = $ext[count[$ext]-1];

	if[$ext == "jpg" || $ext == "jpeg"]
		$im = imagecreatefromjpeg[$tmpname];
	elseif[$ext == "png"]
		$im = imagecreatefrompng[$tmpname];
	elseif[$ext == "gif"]
		$im = imagecreatefromgif[$tmpname];
	
	$x = imagesx[$im];
	$y = imagesy[$im];
	
	if[$x 

Chủ Đề