<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Binary Tides &#187; PHP</title>
	<atom:link href="http://www.binarytides.com/blog/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.binarytides.com/blog</link>
	<description></description>
	<lastBuildDate>Sat, 24 Jul 2010 05:31:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Format numbers to Indian Numerical System</title>
		<link>http://www.binarytides.com/blog/php-format-numbers-to-indian-numerical-system/</link>
		<comments>http://www.binarytides.com/blog/php-format-numbers-to-indian-numerical-system/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 11:20:35 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=124</guid>
		<description><![CDATA[The numerical system used in India differs from the Western system in terms of the placement of the thousands separator.
Example :
Number -&#62; 1000000
Indian System -&#62; 10,00,000   (Ten Lakh)
Western System -&#62; 1,000,000 (1 Million)

While echoing numbers in PHP it is possible to format numbers with such separators using functions like number_format.
To format a number [...]]]></description>
			<content:encoded><![CDATA[<p>The numerical system used in India differs from the Western system in terms of the placement of the thousands separator.</p>
<p>Example :<br />
Number -&gt; 1000000<br />
Indian System -&gt; 10,00,000   (Ten Lakh)<br />
Western System -&gt; 1,000,000 (1 Million)</p>
<p><span id="more-124"></span></p>
<p>While echoing numbers in PHP it is possible to format numbers with such separators using functions like number_format.</p>
<p>To format a number in the Indian Numerical System the code would be :</p>
<pre class="brush: php;">
&lt;?
$amount = '100000';
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount;
?&gt;
</pre>
<p>and the output should be :</p>
<pre class="brush: php;">
1,00,000.00
</pre>
<p>1. Set the locale.<br />
2. Format the number using money_format.</p>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=124&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/php-format-numbers-to-indian-numerical-system/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create short links using bit.ly from php</title>
		<link>http://www.binarytides.com/blog/create-short-links-using-bit-ly-from-php/</link>
		<comments>http://www.binarytides.com/blog/create-short-links-using-bit-ly-from-php/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 12:51:55 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://binarytides.com/blog/?p=1</guid>
		<description><![CDATA[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 = &#34;your_login_name&#34;;
	$bitly_apikey = &#34;your_api_key&#34;;

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

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

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


The above function can be used to shorten a big url , as :

if ($shortlink = get_short_url(&#34;http://www.google.com/&#34;))
{
    echo $shortlink;
}
else
{
 [...]]]></description>
			<content:encoded><![CDATA[<p>bit.ly is a simple url shortner which has an api , which can be called from within php to shorten links.</p>
<p>Code :</p>
<pre class="brush: php;">
function get_short_url($url)
{
	$bitly_login = &quot;your_login_name&quot;;
	$bitly_apikey = &quot;your_api_key&quot;;

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

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

	if ($bitlyinfo['errorCode'] == 0)
	{
		return $bitlyinfo['results'][urldecode($url)]['shortUrl'];
	}
	else
	{
		return false;
	}
}
</pre>
<p><span id="more-1"></span></p>
<p>The above function can be used to shorten a big url , as :</p>
<pre class="brush: php;">
if ($shortlink = get_short_url(&quot;http://www.google.com/&quot;))
{
    echo $shortlink;
}
else
{
    die(&quot;An error shortening the url&quot;);
}
</pre>
<p>The function needs an api key. To get your api key simply register at bit.ly then go to My Account and get your api key.</p>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=1&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/create-short-links-using-bit-ly-from-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parse and Format Dates between PHP and Mysql</title>
		<link>http://www.binarytides.com/blog/parse-and-format-dates-between-php-and-mysql/</link>
		<comments>http://www.binarytides.com/blog/parse-and-format-dates-between-php-and-mysql/#comments</comments>
		<pubDate>Fri, 01 May 2009 16:44:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=56</guid>
		<description><![CDATA[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

Checking a valid date : 
If the date is in format dd-mm-yyyy then a simple way to test its validity would be [...]]]></description>
			<content:encoded><![CDATA[<p>Mysql Database stores dates in the format :<br />
yyyy-mm-dd e.g. 2009-04-15</p>
<p>Whereas humans are more used to the formats like :<br />
dd-mm-yyyy or mm-dd-yyyy e.g. 21-04-2009</p>
<p>And while reading a format like :<br />
dd-Month-yyyy is more usable e.g. 21-Mar-2009</p>
<p><span id="more-56"></span></p>
<p><strong>Checking a valid date : </strong><br />
If the date is in format dd-mm-yyyy then a simple way to test its validity would be :</p>
<pre class="brush: php;">
if(strtotime(implode('-', array_reverse(explode('-', $date)))))
    return true;
</pre>
<p>But the above has an issue that it will report 31-02-2009 as a valid date. The same with strptime.</p>
<p>Another function checkdate is useful for this issue and it also manages leap years.</p>
<pre class="brush: php;">
$datearr = explode('-', $date);
list($d,$m,$y) = $datearr;
if(checkdate($m, $d, $y))
    return true;
</pre>
<p>But a small issue with checkdate is that it reports a date like 01-01-2500 to be valid again (which is , as per what checkdate is supposed to do).</p>
<p>Using strptime to check a date being in a valid dd-mm-yyyy format :</p>
<pre class="brush: php;">
/*
    Check date to be in format dd-mm-yyyy
*/
function valid_date($date) {
    #Check format
    if(!strptime($date , '%d-%m-%Y'))
 return false;

    $parts = explode('-' , $date);
    $d = $parts[0];
    $m = $parts[1];
    $y = $parts[2];
    #Check format of year number
    if(!($y &amp;gt;= 1000 &amp;amp;&amp;amp; $y &amp;lt;= 9999))
 return false;
    return true;
}
</pre>
<p>or more simply :</p>
<pre class="brush: php;">
if(strtotime(implode('-', array_reverse(explode('-', $date)))))
    return true;
else
    return false;
</pre>
<p>Using strptime to convert a date in dd-mm-yyyy format to yyyy-mm-dd the code could be :</p>
<pre class="brush: php;">
/*
    Convert a dd-mm-yyyy into yyyy-mm-dd for mysql insertion
*/
function dbdate($date) {
    $d = strptime($date , '%d-%m-%Y'); #This is the user input format
    if($d)
        $date = ($d['tm_year']+1900).'-'.($d['tm_mon']+1).'-'.$d['tm_mday'];
    else
        $date = '1800-01-01';
    return $date;
}
</pre>
<p>or more simply :</p>
<pre class="brush: php;">
if(strtotime(implode('-', array_reverse(explode('-', $date)))))
    return implode('-', array_reverse(explode('-', $date)));
else
    return '1800-01-01';
</pre>
<p>The above function would convert a date in the format dd-mm-yyyy to yyyy-mm-dd.<br />
e.g. $date = dbdate($date) where $date = &#8220;02-04-2006&#8243;<br />
now $date is ready to be inserted in the mysql database</p>
<p>Checking date format and range :</p>
<pre class="brush: php;">
function valid_date($date , $range=0) {

    $datearr = explode('-', $date);
    if(count($datearr) != 3)
 return false;
    list($d, $m, $y) = $datearr;
    #Check validity
    if($range == 1) {
 #Check date format with range and validity
 if(checkdate($m , $d , $y) and strtotime(&quot;$y-$m-$d&quot;)) #strtotime to invalidate outrange dates like 01-01-2500
     return true;
    }
    else {
 #Check date format and validity and not range
 if(checkdate($m , $d , $y)) #strtotime to invalidate outrange dates like 01-01-2500
     return true;
    }
    return false;
}
</pre>
<p>The above function can just check the date format or the range as well depending on the value of the $range parameter. checkdate() invalidates dates like 31-02-2009 and takes care of leap years as well. Whereas strotime checks the range of the date. The above function checks the format dd-mm-yyyy and dd-mm-yy. A little modification would adapt it to formats dd-mm-yyyy , dd-mm-yy  dd/mm/yyyy and dd/mm/yy :</p>
<pre class="brush: php;">
/*
 Checks the date validity in formats :
 1. dd-mm-yyyy
 2. dd-mm-yy
 3. dd/mm/yyyy
 4. dd/mm/yy

*/
function valid_date($date , $range=0) {
 #For valid dd/mm/yyyy
 $date = str_replace('/', '-', $date);
 #Check
 $datearr = explode('-', $date);
 if(count($datearr) != 3)
  return false;
 list($d, $m, $y) = $datearr;
 #Check validity
 if($range == 1) {
  #Check date format with range and validity
  if(checkdate($m , $d , $y) and strtotime(&quot;$y-$m-$d&quot;)) #strtotime to invalidate outrange dates like 01-01-2500
   return true;
 }
 else {
  #Check date format and validity and not range
  if(checkdate($m , $d , $y)) #strtotime to invalidate outrange dates like 01-01-2500
   return true;
 }
 return false;
}
</pre>
<pre class="brush: php;">
/*
    Give a dd-month-yyyy representation of mysql format yyyy-mm-dd
*/
function showdate($dbdate) {
    $stamp = strtotime($dbdate);
    if($stamp)
        return strftime(&quot;%d-%b-%Y&quot;,$stamp); #This is the user viewing format
    else
        return '';
}
</pre>
<p>While printing dates in a table for viewing purpose a format like 02-Mar-2010 is easily readable and the above function does the same. It converts 2010-03-02 to 02-Mar-2010.</p>
<pre class="brush: php;">
/*
    Convert yyyy-mm-dd to dd-mm-yyyy
*/
function feeddate($dbdate) {
    $stamp = strtotime($dbdate);
    if($stamp)
        return strftime(&quot;%d-%m-%Y&quot;,$stamp); #This is the user viewing format
    else
        return '';
}</pre>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=56&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/parse-and-format-dates-between-php-and-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Database Class to access Mysql</title>
		<link>http://www.binarytides.com/blog/php-database-class-to-access-mysql/</link>
		<comments>http://www.binarytides.com/blog/php-database-class-to-access-mysql/#comments</comments>
		<pubDate>Fri, 01 May 2009 16:26:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=55</guid>
		<description><![CDATA[This is a simple class that evolved out of the php code I wrote so far.
It has the following functions :
1. dbms() &#8211; The constructor to do the initialisation like connecting to the database etc.
2. query($query) &#8211; The method to take a sql string and perform the query.
3. close() &#8211; Close the database connection
4. backup() [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple class that evolved out of the php code I wrote so far.<br />
It has the following functions :</p>
<p>1. <strong>dbms()</strong> &#8211; The constructor to do the initialisation like connecting to the database etc.<br />
2. <strong>query($query)</strong> &#8211; The method to take a sql string and perform the query.<br />
3. <strong>close()</strong> &#8211; Close the database connection<br />
4. <strong>backup()</strong> &#8211; Create a backup of the database as an sql file and then zip it.<br />
5. <strong>restore()</strong> &#8211; Restore database from a zip file which was created using the previous function backup()<br />
6. <strong>get_last_insert_id()</strong> &#8211; Get the id from the last INSERT statement using the LAST_INSERT_ID() function of mysql database.<br />
7. <strong>indb($table , $field , $value)</strong> &#8211; Checks whether a value of $value already exists in the $field column of a table $table. e.g. checking whether a username or an email already exists or not.</p>
<p><span id="more-55"></span></p>
<p><strong>Code</strong> :</p>
<pre class="brush: php;">
&lt;?php
class dbms {
 #DB Configuration parameters
 var $host = 'localhost';
 var $username = 'root';
 var $password = 'secret';
 var $db = 'projects_bookmark';
 #Connection object to be used in this object
 var $connection;
 #Constructor
 function dbms() {
  #Attempt to connect to database
  $this-&gt;connection = mysql_connect($this-&gt;host, $this-&gt;username, $this-&gt;password);
  #Check connection validity
  if (!$this-&gt;connection) {
   die (&quot;Could not connect to the database:
&quot;. mysql_error($this-&gt;connection));
  }
  #If connection was successful then select the database
  $db_select = mysql_select_db( $this-&gt;db , $this-&gt;connection );
  #Check database selection validity
  if (!$db_select) {
   die (&quot;Could not select the database:
&quot;. mysql_error($this-&gt;connection));
  }
 }
 #Function to perform a query
 function query($query) {
  $result = mysql_query( $query , $this-&gt;connection);
  if (!$result) {
   die (&quot;Could not query the database:
 $query &quot;. mysql_error());
  }
  return $result;
 }
 #Method to close
 function close() {
  mysql_close($this-&gt;connection);
 }
 #Function to backup database to a zip file
 function backup() {
  $suffix = time();
  #Execute the command to create backup sql file
  exec(&quot;mysqldump --user={$this-&gt;username} --password={$this-&gt;password} --quick --add-drop-table --add-locks --extended-insert --lock-tables --all {$this-&gt;db} &gt; backups/backup.sql&quot;);

  #Now zip that file
  $zip = new ZipArchive();
  $filename = &quot;backups/backup-$suffix.zip&quot;;
  if ($zip-&gt;open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
   exit(&quot;cannot open &lt;$filename&gt;\n&quot;);
  }
  $zip-&gt;addFile(&quot;backups/backup.sql&quot; , &quot;backup.sql&quot;);
  $zip-&gt;close();
  #Now delete the .sql file without any warning
  @unlink(&quot;backups/backup.sql&quot;);
  #Return the path to the zip backup file
  return &quot;backups/backup-$suffix.zip&quot;;
 }
 #Function to restore from a file
 function restore($path) {
  $f = fopen('restore/temp.sql' , 'w+');
  if(!$f) {
   echo &quot;Error While Restoring Database&quot;;
   return;
  }
  $zip = new ZipArchive();
  if ($zip-&gt;open($path) === TRUE) {
   #Get the backup content
   $sql = $zip-&gt;getFromName('backup.sql');
   #Close the Zip File
   $zip-&gt;close();
   #Prepare the sql file
   fwrite($f , $sql);
   fclose($f);

   #Now restore from the .sql file
   $command = &quot;mysql --user={$this-&gt;username} --password={$this-&gt;password} --database={$this-&gt;db} &lt; restore/temp.sql&quot;;
   exec($command);

   #Delete temporary files without any warning
   @unlink('restore/temp.sql');
  }
  else {
   echo 'Failed';
  }
 }
 #Function to get the last insert ID
 function get_last_insert_id() {
  $results = $this-&gt;query(&quot;SELECT LAST_INSERT_ID()&quot;);
  $r = mysql_fetch_array($results);
  return $r[0];
 }
 #Function to check whether a value exists in some column of some table
 function indb($table , $field , $value) {
  $db = new dbms();
  $results = $db-&gt;query(&quot;SELECT $field FROM $table WHERE $field = '$value' &quot;);
  if(mysql_num_rows($results))
   return true;
  else
   return false;
 }
}
?&gt;
</pre>
<p>Simple!</p>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=55&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/php-database-class-to-access-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Restore Mysql Database from a sql or zip file using PHP</title>
		<link>http://www.binarytides.com/blog/restore-mysql-database-from-a-sql-or-zip-file-using-php/</link>
		<comments>http://www.binarytides.com/blog/restore-mysql-database-from-a-sql-or-zip-file-using-php/#comments</comments>
		<pubDate>Fri, 01 May 2009 12:10:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=54</guid>
		<description><![CDATA[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.

In this example the same zip file would be used to restore the database from it.
Code  :

#Function to restore from a file
function restore($path) {
  $f = fopen('restore/temp.sql' , 'w+');
 [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous <a href="http://prasshhant.blogspot.com/2009/05/backup-mysql-database-with-php-and-zip.html">post</a> we saw how the backup of a database can be taken in the form of a sql file and then zipped.</p>
<p><span id="more-54"></span></p>
<p>In this example the same zip file would be used to restore the database from it.</p>
<p><strong>Code </strong> :</p>
<pre class="brush: php;">
#Function to restore from a file
function restore($path) {
  $f = fopen('restore/temp.sql' , 'w+');
  if(!$f) {
   echo &quot;Error While Restoring Database&quot;;
   return;
  }
  $zip = new ZipArchive();
  if ($zip-&gt;open($path) === TRUE) {
   #Get the backup content
   $sql = $zip-&gt;getFromName('backup.sql');
   #Close the Zip File
   $zip-&gt;close();
   #Prepare the sql file
   fwrite($f , $sql);
   fclose($f);

   #Now restore from the .sql file
   $command = &quot;mysql --user={$username} --password={$password} --database={$db} &lt; restore/temp.sql&quot;;
   exec($command);

   #Delete temporary files without any warning
   @unlink('restore/temp.sql');
  }
  else {
   echo 'Failed';
  }
}   
</pre>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=54&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/restore-mysql-database-from-a-sql-or-zip-file-using-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Backup mysql database with php and zip it</title>
		<link>http://www.binarytides.com/blog/backup-mysql-database-with-php-and-zip-it/</link>
		<comments>http://www.binarytides.com/blog/backup-mysql-database-with-php-and-zip-it/#comments</comments>
		<pubDate>Fri, 01 May 2009 11:59:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=53</guid>
		<description><![CDATA[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 &#8211;user={$username} &#8211;password={$password} &#8211;quick &#8211;add-drop-table &#8211;add-locks &#8211;extended-insert &#8211;lock-tables &#8211;all {$db} &#62; backups/backup.sql

$username &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>PHP can be used to backup a mysql database and make a zip file of it.</p>
<p>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 :<br />
<strong>mysqldump &#8211;user={$username} &#8211;password={$password} &#8211;quick &#8211;add-drop-table &#8211;add-locks &#8211;extended-insert &#8211;lock-tables &#8211;all {$db} &gt; backups/backup.sql</strong></p>
<p><span id="more-53"></span></p>
<p>$username &#8211; mysql username<br />
$password &#8211; mysql password<br />
$db &#8211; name of the database to backup</p>
<p>In the above example backups/backup.sql is the path to the sql file. Next this backup.sql file is zipped using the ZipArchive library class of php.</p>
<p><strong>Code</strong> :</p>
<pre class="brush: php;">
#Function to backup database to a zip file
function backup()
{
  $suffix = time();
  #Execute the command to create backup sql file
  exec(&quot;mysqldump --user={$username} --password={$password} --quick --add-drop-table --add-locks --extended-insert --lock-tables --all {$db} &gt; backups/backup.sql&quot;);

  #Now zip that file
  $zip = new ZipArchive();
  $filename = &quot;backups/backup-$suffix.zip&quot;;
  if ($zip-&gt;open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
   exit(&quot;cannot open &lt;$filename&gt;\n&quot;);
  }
  $zip-&gt;addFile(&quot;backups/backup.sql&quot; , &quot;backup.sql&quot;);
  $zip-&gt;close();
  #Now delete the .sql file without any warning
  @unlink(&quot;backups/backup.sql&quot;);
  #Return the path to the zip backup file
  return &quot;backups/backup-$suffix.zip&quot;;
} 
</pre>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=53&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/backup-mysql-database-with-php-and-zip-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP parse text and change urls into links</title>
		<link>http://www.binarytides.com/blog/php-parse-text-and-change-urls-into-links/</link>
		<comments>http://www.binarytides.com/blog/php-parse-text-and-change-urls-into-links/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 17:00:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=50</guid>
		<description><![CDATA[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('&#124;http://([a-zA-Z0-9-\./]+)&#124;', '&#38;lt;a href=&#38;quot;http://$1&#38;quot;&#38;gt;$1&#38;lt;/a&#38;gt;', $str);
    $str = preg_replace('/(([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6})/', '&#38;lt;a href=&#38;quot;mailto:$1&#38;quot;&#38;gt;$1&#38;lt;/a&#38;gt;', $str);
    return $str;
}
?&#38;gt;

&#60;span id=&#34;more-50&#34;&#62;&#60;/span&#62;

]]></description>
			<content:encoded><![CDATA[<p>Regex can be used to convert all urls in a plain text into links i.e. with anchor tags.<br />
<strong>Code</strong> :</p>
<pre class="brush: php;">
function parse_links($str)
{
    $str = str_replace('www.', 'http://www.', $str);
    $str = preg_replace('|http://([a-zA-Z0-9-\./]+)|', '&amp;lt;a href=&amp;quot;http://$1&amp;quot;&amp;gt;$1&amp;lt;/a&amp;gt;', $str);
    $str = preg_replace('/(([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6})/', '&amp;lt;a href=&amp;quot;mailto:$1&amp;quot;&amp;gt;$1&amp;lt;/a&amp;gt;', $str);
    return $str;
}
?&amp;gt;

&lt;span id=&quot;more-50&quot;&gt;&lt;/span&gt;
</pre>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=50&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/php-parse-text-and-change-urls-into-links/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Send mail with PHP</title>
		<link>http://www.binarytides.com/blog/send-mail-with-php/</link>
		<comments>http://www.binarytides.com/blog/send-mail-with-php/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 13:36:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=47</guid>
		<description><![CDATA[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';
}


Additional headers can be set like this :

$headers = 'From: abcd@example.com' . &#34;\r\n&#34; .
'Reply-To: abcd@example.com' . &#34;\r\n&#34; .
'X-Mailer: PHP/' . phpversion();

]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://in.php.net/manual/en/function.mail.php">mail</a> function of php can be used to send mails.</p>
<p>E.g.</p>
<pre class="brush: php;">
$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';
}
</pre>
<p><span id="more-47"></span></p>
<p>Additional headers can be set like this :</p>
<pre class="brush: php;">
$headers = 'From: abcd@example.com' . &quot;\r\n&quot; .
'Reply-To: abcd@example.com' . &quot;\r\n&quot; .
'X-Mailer: PHP/' . phpversion();
</pre>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=47&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/send-mail-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Image Cropper with Mootools and PHP</title>
		<link>http://www.binarytides.com/blog/javascript-image-cropper-with-mootools-and-php/</link>
		<comments>http://www.binarytides.com/blog/javascript-image-cropper-with-mootools-and-php/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 13:26:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[mootools]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=46</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.nwhite.net/MooCrop/">MooCrop</a> 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 crop region ; these values can be used by a php script to crop an image.</p>
<p><span id="more-46"></span></p>
<p>So these values can be obtained in the script like this :<br />$x = $_GET['x'];<br />$y = $_GET['y'];<br />$h = $_GET['h'];<br />$w = $_GET['w'];</p>
<p>For e.g. if its a jpg image then</p>
<p>$src = imagecreatefromjpeg($path);<br />where $path has the path to the image.</p>
<p>Now create a new image with height and width<br />$dest = imagecreatetruecolor($w, $h);<br />imagecopy($dest, $src, 0, 0, $x, $y, $w, $h);</p>
<p>Now overwrite the existing file :<br />imagejpeg($dest , $path);</p>
<p>You may need to do a chmod over the new file.<br />chmod($path , 0755);</p>
<p>Free memory<br />imagedestroy($dest);<br />imagedestroy($src);</p>
<p>Now $path is the new image which is cropped as per the values of x , y , h , w</p>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=46&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/javascript-image-cropper-with-mootools-and-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Run PHP scripts automatically at intervals &#8211; use Cron Jobs</title>
		<link>http://www.binarytides.com/blog/run-php-scripts-automatically-at-intervals-use-cron-jobs/</link>
		<comments>http://www.binarytides.com/blog/run-php-scripts-automatically-at-intervals-use-cron-jobs/#comments</comments>
		<pubDate>Sun, 22 Mar 2009 12:21:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Cron]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=41</guid>
		<description><![CDATA[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.

To automate a script is simple. The webhost must provide cron facility. Cron is a utility which can be used to [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p><span id="more-41"></span></p>
<p>To automate a script is simple. The webhost must provide cron facility. Cron is a utility which can be used to execute a set of commands at a given schedule of time.</p>
<p>For e.g. if a php script called regular.php is to be executed regularly every day or every month or on certain weekdays then cron can be configured to execute the script at defined schedules and hence automation is achieved. Since web hosts are alive and online most of the times the automation process is more useful when used on a web server rather than a home pc.</p>
<p>Cron can be configured from Cpanel and via ssh terminal as well.</p>
<p>To configure cron from ssh terminal simply login to your webspace using ssh.</p>
<p>The command crontab -l will show what schedules have already been made or what tasks are to execute according to current cron settings.</p>
<p>Cron settings consist of lines like this</p>
<p><strong>* * * * * /command/to/execute</strong></p>
<p>The 5 stars indicate the time to execute and the /command/to/execute is the path to the script or command which should be executed at that point of time.</p>
<p>If you put the regular.php script in the www folder on your webhost then the path could be :<br />
<strong> </strong></p>
<p><strong>/home/username/www/regular.php</strong></p>
<p>So the command to execute it would be</p>
<p><strong>php /home/username/www/regular.php</strong></p>
<p>The above command needs a time schedule to run.</p>
<p>The 5 stars indicate the following</p>
<p>Minute Hour Day Month Day    &#8212; in that order</p>
<p>Minute &#8211; 0-59<br />
Hour &#8211; 0-23<br />
Day &#8211; 1-31<br />
Month &#8211; 1-12<br />
Weekday 0-6  (o= Sunday)<br />
* means any</p>
<p>So to execute a script everyday<br />
<strong> 0 0 * * *</strong> &#8211; means At 00:00 anyday anymonth anyweekday</p>
<p>So the cron line would be<br />
<strong> 0 0 * * * php /home/username/www/regular.php</strong></p>
<p>All such cron schedule lines can be written in a txt file and this cron file can be given to crontab to set the schedules using the following command :</p>
<pre class="brush: cpp;">
crontab crontasks.txt
</pre>
<p>Another line that can be added on the top of the crontasks.txt file is<br />
<strong> MAILTO=user@site.com</strong></p>
<p>and this is the email address to which the execution results shall be mailed.</p>
<p>So the crontasks.txt file can look like this :</p>
<p>MAILTO=user@site.com<br />
0 0 * * * php /home/username/www/regular.php</p>
<p>should be saved and the command :</p>
<pre class="brush: cpp;">
$crontab crontasks.txt
</pre>
<p>would set it.</p>
<p>Before setting a new crontask set you may want to backup the previous set of cron tasks :</p>
<pre class="brush: cpp;">
$crontab -l &gt; oldcrontasks.txt
</pre>
<p>List current cron jobs :</p>
<pre class="brush: cpp;">
$crontab -l
</pre>
<p>Remove all cron jobs :</p>
<pre class="brush: cpp;">
crontab -r
</pre>
<p>So using crontab any php script or any other script or command can be executed automatically on server side.</p>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=41&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/run-php-scripts-automatically-at-intervals-use-cron-jobs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
