Skip to content

Instantly share code, notes, and snippets.

@cherifgx
cherifgx / Small files download
Created March 26, 2013 05:57
Small files download to your server
<?php
file_put_contents("Tmpfile.zip", file_get_contents("http://someurl/file.zip"));
@cherifgx
cherifgx / Detect AJAX Request
Created March 26, 2013 05:08
Most of the JavaScript frameworks like jQuery, mootools send and additional HTTP_X_REQUESTED_WITH header when they make an AJAX request, so that you can detect AJAX request on server side.
<?php
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
//If AJAX Request Then
}else{
//something else
}
@cherifgx
cherifgx / Resize Images on the fly
Created March 26, 2013 05:08
Creating thumbnails of the images is required many a times, this code will be useful to know about the logic of thumbnail generation.
<?php
/**********************
*@filename - path to the image
*@tmpname - temporary path to thumbnail
*@xmax - max width
*@ymax - max height
*/
function resize_image($filename, $tmpname, $xmax, $ymax)
{
@cherifgx
cherifgx / Convert URLs within String into hyperlinks
Created March 26, 2013 05:08
This function converts URLs and e-mail addresses within a string into clickable hyperlinks.
@cherifgx
cherifgx / Prepend http to a URL
Created March 26, 2013 05:07
Some times you need to accept some url as input but users seldom add http:// to it, this code will add http:// to the URL if it’s not there.
<?php
if (!preg_match("/^(http|ftp):/", $_POST['url'])) {
$_POST['url'] = 'http://'.$_POST['url'];
}
<?php
/**********************
*@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) {
@cherifgx
cherifgx / Truncate Text at Word Break
Created March 26, 2013 05:07
This function will truncate strings only at word breaks which can be used to show a teaser for complete article without breaking words.
<?php
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
function myTruncate($string, $limit, $break=".", $pad="...") {
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit)
return $string;
// is $break present between $limit and the end of the string?
if(false !== ($breakpoint = strpos($string, $break, $limit))) {
@cherifgx
cherifgx / Use Gravatars in Your Application
Created March 26, 2013 05:04
With ever increasing popularity of WordPress, Gravatars have become quite popular. It is very easy to integrate them into your application as they provide a very easy to use API.
<?php
/******************
*@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 '<img src="http://www.gravatar.com/avatar.php?gravatar_id='.md5($email).
@cherifgx
cherifgx / Find Similarity Between Two Strings
Created March 26, 2013 05:03
PHP includes a function similar_text very rarely used but quite useful that compares two strings and returns the percentage of similarity between two.
<?php
similar_text($string1, $string2, $percent);
//$percent will have the percentage of similarity
<?php
function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 30 )
{
$minimumCount = min($data);
$maximumCount = max($data);
$spread = $maximumCount - $minimumCount;
$cloudHTML = '';
$cloudTags = array();