$ sudo du /backup/ -h | sort -h
Friday, May 16, 2014
List files in sorted order using 'du'
Listing of all the files (and subdirectories) within a directory along with their sizes in sorted and readable format.
Saturday, December 14, 2013
Find and Replace with Regular Expressions in PHP
If we want to remove a specific part from a string, we can use regular expressions in php
Result : What is Your Name
Here removes serial number from a string. eg : 12. Pattern match search for [\d]+[\.] . This means any digit followed by a dot(.)
<?php
$oldstring="12. What is Your Name";
$newstring=preg_replace('/[\d]+[\.]+[\s?]/', '',$oldstring);
echo $newstring;
?> Result : What is Your Name
Here removes serial number from a string. eg : 12. Pattern match search for [\d]+[\.] . This means any digit followed by a dot(.)
Wednesday, August 21, 2013
Age Calculation in MySQL based on Date of Birth Field
To find out age in mysql we can use two methods. (Assume that DOB is the field name in table)
1)
The above query returns the exact age. (looking for birth date)
- 2013-08-22 - 2008-11-02 returns 4
2)
The above query returns the age difference by year (looking for year only)
- 2013-08-22 - 2008-11-02 returns 5
1)
SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(DOB, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(DOB, '00-%m-%d')) AS age FROM students The above query returns the exact age. (looking for birth date)
- 2013-08-22 - 2008-11-02 returns 4
2)
SELECT YEAR( CURDATE( ) ) - YEAR( DOB ) AS age FROM students.The above query returns the age difference by year (looking for year only)
- 2013-08-22 - 2008-11-02 returns 5
Monday, August 5, 2013
MySQL Command Line Backup
From Shell prompt Enter the following.
This command will make mysql backup and compress it in the name format dbname-06-08-2013-17:17:34.gz
$ mysqldump -u root -pmqr dbname |gzip -9 > /backup/koha/dbname-$(date +"%d-%m-%Y-%H:%M:%S").gzThis command will make mysql backup and compress it in the name format dbname-06-08-2013-17:17:34.gz
Thursday, August 30, 2012
How to get full URL in PHP
To get full url of running script, we use the following two functions in PHP
- $_SERVER['HTTP_HOST']
- $_SERVER['PHP_SELF']
Examples
Consider my url string is : http://mysite.com/products/booking.php
<?php echo $_SERVER['HTTP_HOST']; ?>
Output : mysite.com
echo $_SERVER['REQUEST_URI'];
Output : /products/booking.php
<?php echo 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);?>
Output : http://mysite.com/products
Thursday, July 19, 2012
Email validation using Regular Expression
Following code shows how to validate email in php.
<?php
// chek email validation
if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$_POST['email']))
{
echo "invalid Email Address";
}
?>
Detect url contains query string ?
parse_url function is used to break the url components
The following code reads url query string and convert into array.
More examples
Output : 'mode=edit&id=34'
Output : 'user'
Output : 'http'
Output : 'example.com'
Output : 88
This
function parses a URL and returns an associative array containing any
of the various components of the URL that are present.
- scheme - e.g. http
- host
- port
- user
- pass
- path
- query - after the question mark ?
- fragment - after the hashmark #
The following code reads url query string and convert into array.
<?php
$queries= parse_url($_SERVER['REQUEST_URI'],PHP_URL_QUERY); // splits url query string into array
$queries_elements = split('&', $queries);
//converts to array
for($i = 0; $i != sizeof($queries_elements); $i++)
{
$queries = split('=', $queries_elements[$i]);
//assigns to new array
$params[$queries[0]]=$queries[1];
}
var_dump($params);?> More examples
echo parse_url('http://example.com?mode=edit&id=34', PHP_URL_QUERY);Output : 'mode=edit&id=34'
echo parse_url('http://example.com?mode=edit&id=34#user', PHP_URL_FRAGMENT)Output : 'user'
echo parse_url('http://example.com', PHP_URL_SCHEME);Output : 'http'
echo parse_url('http://example.com?mode=edit&id=34#user', PHP_URL_HOST);Output : 'example.com'
echo parse_url('http://example.com:88?id=34', PHP_URL_PORT);Output : 88
Subscribe to:
Posts (Atom)