Archive for the 'PHP' Category

PHP Format numbers to Indian Numerical System

The numerical system used in India differs from the Western system in terms of the placement of the thousands separator.
Example :
Number -> 1000000
Indian System -> 10,00,000 (Ten Lakh)
Western System -> 1,000,000 (1 Million)

Create short links using bit.ly from php

bit.ly is a simple url shortner which has an api , which can be called from within php to shorten links.
Code :

function get_short_url($url)
{
$bitly_login = "your_login_name";
$bitly_apikey = "your_api_key";

$api_call = file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl=".$url."&login=".$bitly_login."&apiKey=".$bitly_apikey);

$bitlyinfo=json_decode(utf8_encode($api_call),true);

if ($bitlyinfo['errorCode'] == 0)
{
return $bitlyinfo['results'][urldecode($url)]['shortUrl'];
}
else
{
return false;
}
}

Parse and Format Dates between PHP and Mysql

Mysql Database stores dates in the format :
yyyy-mm-dd e.g. 2009-04-15
Whereas humans are more used to the formats like :
dd-mm-yyyy or mm-dd-yyyy e.g. 21-04-2009
And while reading a format like :
dd-Month-yyyy is more usable e.g. 21-Mar-2009

PHP Database Class to access Mysql

This is a simple class that evolved out of the php code I wrote so far.
It has the following functions :
1. dbms() – The constructor to do the initialisation like connecting to the database etc.
2. query($query) – The method to take a sql string and perform the query.
3. close() – Close the database connection
4. backup() [...]

Restore Mysql Database from a sql or zip file using PHP

In the previous post we saw how the backup of a database can be taken in the form of a sql file and then zipped.

Backup mysql database with php and zip it

PHP can be used to backup a mysql database and make a zip file of it.
mysqldump is the utility which can be used to perform this function of backing up a mysql database as sql file. The command would be like this :
mysqldump –user={$username} –password={$password} –quick –add-drop-table –add-locks –extended-insert –lock-tables –all {$db} > backups/backup.sql

PHP parse text and change urls into links

Regex can be used to convert all urls in a plain text into links i.e. with anchor tags.
Code :
function parse_links($str)
{
$str = str_replace(’www.’, ‘http://www.’, $str);
$str = preg_replace(’|http://([a-zA-Z0-9-\./]+)|’, ‘<a href="http://$1">$1</a>’, $str);
$str = preg_replace(’/(([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6})/’, ‘<a href="mailto:$1">$1</a>’, $str);
return $str;
}
?>

Send mail with PHP

The mail function of php can be used to send mails.
E.g.

$to = ‘recipient@example.com’;
$subject = ‘Hello This is the Subject’;
$body = ‘Hello This is the Body’;
if (mail($to, $subject, $body))
{
echo ‘Message Delivered’;
}
else
{
echo ‘Message delivery failed’;
}

Javascript Image Cropper with Mootools and PHP

MooCrop is a library which can be used to create a cropping interface in your webpage. With this library after a crop region is selected a php script can be called to crop the image on serverside. The moocrop interface provides four values that is the width , height , left and top of the [...]

Run PHP scripts automatically at intervals – use Cron Jobs

Automation may be needed in to order to run certain scripts at regular intervals for tasks like scraping , extracting or crawling data from web or do some regular tasks like sending emails or whatever.