30 December 2013

Regular Expressions Symbology

Regular Expressions Anchors


^ Start of string, or start of line in multi-line pattern
A Start of string
$ End of string, or end of line in multi-line pattern
Z End of string
b Word boundary
B Not word boundary
< Start of word
> End of word

Regular Expressions Character Classes

c Control character
s White space
S Not white space
d Digit
D Not digit
w Word
W Not word
x Hexade­cimal digit
O Octal digit

Regular Expressions POSIX

[:upper:] Upper case letters
[:lower:] Lower case letters
[:alpha:] All letters
[:alnum:] Digits and letters
[:digit:] Digits
[:xdigit:] Hexade­cimal digits
[:punct:] Punctu­ation
[:blank:] Space and tab
[:space:] Blank characters
[:cntrl:] Control characters
[:graph:] Printed characters
[:print:] Printed characters and spaces
[:word:] Digits, letters and underscore

Regular Expressions Assertions

?= Lookahead assertion
?! Negative lookahead
?<= Lookbehind assertion
?!= or ?<! Negative lookbehind
?> Once-only Subexp­ression
?() Condition [if then]
?()| Condition [if then else]
?# Comment

Regular Expressions Quantifiers

* 0 or more
+ 1 or more
? 0 or 1
{3} Exactly 3
{3,} 3 or more
{3,5} 3, 4 or 5

Add a ? to a quantifier to make it ungreedy.

Regular Expressions Escape Sequences

Escape following character
Q Begin literal sequence
E End literal sequence

"­Esc­api­ng" is a way of treating characters which have a special meaning in regular expres­sions literally, rather than as special charac­ters.

Regular Expression Common Metacharacters

^ [ .
$ { *
( +
) | ?
< >

The escape character is usually the backslash - .

Regular Expressions Special Characters

n New line
r Carriage return
t Tab
v Vertical tab
f Form feed
xxx Octal character xxx
xhh Hex character hh

Regular Expressions Groups and Ranges

. Any character except new line (n)
(a|b) a or b
(...) Group
(?:...) Passive (non-c­apt­uring) group
[abc] Range (a or b or c)
[^abc] Not a or b or c
[a-q] Letter from a to q
[A-Q] Upper case letter from A to Q
[0-7] Digit from 0 to 7
n nth group/­sub­pattern

Ranges are inclusive.

Regular Expressions Pattern Modifiers

g Global match
i Case-i­nse­nsitive
m Multiple lines
s Treat string as single line
x Allow comments and white space in pattern
e Evaluate replac­ement
U Ungreedy pattern

Regular Expressions String Replacement

$n nth non-pa­ssive group
$2 "­xyz­" in /^(abc­(xy­z))$/
$1 "­xyz­" in /^(?:a­bc)­(xyz)$/
$` Before matched string
$' After matched string
$+ Last matched string
$& Entire matched string

Some regex implem­ent­ations use instead of $.

27 December 2013

Check Website Is Available(Running) Or Not

This script will help you to check Domain Status (Website is available or not) using cURL...
This function will return True if Domain(Website) is Available, return False if not Available...

/**
 * To Check Domain Status (available or not).
 * @param $domain --> Site's Url.
 * @return true --> if domain is available(Running), false --> if not available.
 */
function checkDomainStatus($domain)
{
 //check, if a valid url is provided
 if(!filter_var($domain, FILTER_VALIDATE_URL)){
  return false;
 }

 //initialize curl
 $curlInit = curl_init($domain);
 curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
 curl_setopt($curlInit,CURLOPT_HEADER,true);
 curl_setopt($curlInit,CURLOPT_NOBODY,true);
 curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);

 //get answer
 $response = curl_exec($curlInit);
 curl_close($curlInit);
 if ($response){
  return true;
 }
 return false;
}

How to Extract Email Address From String

Here is a function to Extract all the Email Addresses from String...
This function will return an array of found email ids...

function extractEmailsFromString($string)
{
 $emails = array();
 $string = str_replace("rn",' ',$string);
 $string = str_replace("n",' ',$string);
 foreach(preg_split('/s/', $string) as $token)
 {
  $email = filter_var(filter_var($token, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);
  if ($email !== false) {
   $emails[] = $email;
  }
 }
 return $emails;
}

24 December 2013

Read an xml file with jQuery/JavaScript

First of all, Making sure you have jQuery included on your page..
<script type="text/javascript" src="http://YourDomain.com/jquery.js" ></script>


And than, Take a look into your XML file, and change script code according you XML format.
My XML file format is like this:-
<item>
 <link>http://paghdarakshay.wordpress.com/</link>
 <title>Code Solution</title>
 <description>Code Solution</description>
</item>



Now, Here is the script to read an XML:-
<script type="text/javascript">
$(document).ready(function(){
 //Asynchronously retrieve the xml file contents
 $.ajax({
  type: "POST",
  url: "sitemap.xml",
  dataType: "xml",
  success: function(xml) { //Upon successful retrieval
   //Iterate through the all the nodes/items
   $(xml).find('item').each(function(e){ //For each item, perform the following
    var id = e;
    var title = $(this).find('title').text(); //Find the child element of "item" called title
    var url = $(this).find('link').text(); //Find the child element of "item" called link
    var description = $(this).find('description').text();
    //Write out a custom link with the values above
    $('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');
    //Establish the "description" node/element as the this value below, to be referenced fro child nodes/elements
    $('<div class="description"></div>').html(description).appendTo('#link_'+id);
   });
  }
 });
});
</script>


You can also alter the script according to your purpose.
Note:- Make sure your XML file format and jQuery variables are proper...

Find Country Name By IP Address in PHP

This function will help you to find country from IP address...
Function will return country name and city name using the www.geoplugin.net service...

function getLocationInfoByIp()
{
 $client_ip  = @$_SERVER['HTTP_CLIENT_IP'];
 $forward_ip = @$_SERVER['HTTP_X_FORWARDED_FOR'];
 $remote_ip  = @$_SERVER['REMOTE_ADDR'];
 $return_data  = array('country'=>'', 'city'=>'');
 if(filter_var($client_ip, FILTER_VALIDATE_IP))
 {
  $ip_addr = $client_ip;
 }
 elseif(filter_var($forward_ip, FILTER_VALIDATE_IP))
 {
  $ip_addr = $forward_ip;
 }
 else
 {
  $ip_addr = $remote_ip;
 }
 $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip_addr));
 if($ip_data && $ip_data->geoplugin_countryName != null)
 {
  $return_data['country'] = $ip_data->geoplugin_countryCode;
  $return_data['city'] = $ip_data->geoplugin_city;
 }
 return $return_data;
}

Country list with country code and continent

Here's an array of country list with country code and continent!

$countries = array(
 "AF" => array("country" => "Afghanistan", "continent" => "Asia"),
 "AX" => array("country" => "Ă…land Islands", "continent" => "Europe"),
 "AL" => array("country" => "Albania", "continent" => "Europe"),
 "DZ" => array("country" => "Algeria", "continent" => "Africa"),
 "AS" => array("country" => "American Samoa", "continent" => "Oceania"),
 "AD" => array("country" => "Andorra", "continent" => "Europe"),
 "AO" => array("country" => "Angola", "continent" => "Africa"),
 "AI" => array("country" => "Anguilla", "continent" => "North America"),
 "AQ" => array("country" => "Antarctica", "continent" => "Antarctica"),
 "AG" => array("country" => "Antigua and Barbuda", "continent" => "North America"),
 "AR" => array("country" => "Argentina", "continent" => "South America"),
 "AM" => array("country" => "Armenia", "continent" => "Asia"),
 "AW" => array("country" => "Aruba", "continent" => "North America"),
 "AU" => array("country" => "Australia", "continent" => "Oceania"),
 "AT" => array("country" => "Austria", "continent" => "Europe"),
 "AZ" => array("country" => "Azerbaijan", "continent" => "Asia"),
 "BS" => array("country" => "Bahamas", "continent" => "North America"),
 "BH" => array("country" => "Bahrain", "continent" => "Asia"),
 "BD" => array("country" => "Bangladesh", "continent" => "Asia"),
 "BB" => array("country" => "Barbados", "continent" => "North America"),
 "BY" => array("country" => "Belarus", "continent" => "Europe"),
 "BE" => array("country" => "Belgium", "continent" => "Europe"),
 "BZ" => array("country" => "Belize", "continent" => "North America"),
 "BJ" => array("country" => "Benin", "continent" => "Africa"),
 "BM" => array("country" => "Bermuda", "continent" => "North America"),
 "BT" => array("country" => "Bhutan", "continent" => "Asia"),
 "BO" => array("country" => "Bolivia", "continent" => "South America"),
 "BA" => array("country" => "Bosnia and Herzegovina", "continent" => "Europe"),
 "BW" => array("country" => "Botswana", "continent" => "Africa"),
 "BV" => array("country" => "Bouvet Island", "continent" => "Antarctica"),
 "BR" => array("country" => "Brazil", "continent" => "South America"),
 "IO" => array("country" => "British Indian Ocean Territory", "continent" => "Asia"),
 "BN" => array("country" => "Brunei Darussalam", "continent" => "Asia"),
 "BG" => array("country" => "Bulgaria", "continent" => "Europe"),
 "BF" => array("country" => "Burkina Faso", "continent" => "Africa"),
 "BI" => array("country" => "Burundi", "continent" => "Africa"),
 "KH" => array("country" => "Cambodia", "continent" => "Asia"),
 "CM" => array("country" => "Cameroon", "continent" => "Africa"),
 "CA" => array("country" => "Canada", "continent" => "North America"),
 "CV" => array("country" => "Cape Verde", "continent" => "Africa"),
 "KY" => array("country" => "Cayman Islands", "continent" => "North America"),
 "CF" => array("country" => "Central African Republic", "continent" => "Africa"),
 "TD" => array("country" => "Chad", "continent" => "Africa"),
 "CL" => array("country" => "Chile", "continent" => "South America"),
 "CN" => array("country" => "China", "continent" => "Asia"),
 "CX" => array("country" => "Christmas Island", "continent" => "Asia"),
 "CC" => array("country" => "Cocos (Keeling) Islands", "continent" => "Asia"),
 "CO" => array("country" => "Colombia", "continent" => "South America"),
 "KM" => array("country" => "Comoros", "continent" => "Africa"),
 "CG" => array("country" => "Congo", "continent" => "Africa"),
 "CD" => array("country" => "The Democratic Republic of The Congo", "continent" => "Africa"),
 "CK" => array("country" => "Cook Islands", "continent" => "Oceania"),
 "CR" => array("country" => "Costa Rica", "continent" => "North America"),
 "CI" => array("country" => "Cote D'ivoire", "continent" => "Africa"),
 "HR" => array("country" => "Croatia", "continent" => "Europe"),
 "CU" => array("country" => "Cuba", "continent" => "North America"),
 "CY" => array("country" => "Cyprus", "continent" => "Asia"),
 "CZ" => array("country" => "Czech Republic", "continent" => "Europe"),
 "DK" => array("country" => "Denmark", "continent" => "Europe"),
 "DJ" => array("country" => "Djibouti", "continent" => "Africa"),
 "DM" => array("country" => "Dominica", "continent" => "North America"),
 "DO" => array("country" => "Dominican Republic", "continent" => "North America"),
 "EC" => array("country" => "Ecuador", "continent" => "South America"),
 "EG" => array("country" => "Egypt", "continent" => "Africa"),
 "SV" => array("country" => "El Salvador", "continent" => "North America"),
 "GQ" => array("country" => "Equatorial Guinea", "continent" => "Africa"),
 "ER" => array("country" => "Eritrea", "continent" => "Africa"),
 "EE" => array("country" => "Estonia", "continent" => "Europe"),
 "ET" => array("country" => "Ethiopia", "continent" => "Africa"),
 "FK" => array("country" => "Falkland Islands (Malvinas)", "continent" => "South America"),
 "FO" => array("country" => "Faroe Islands", "continent" => "Europe"),
 "FJ" => array("country" => "Fiji", "continent" => "Oceania"),
 "FI" => array("country" => "Finland", "continent" => "Europe"),
 "FR" => array("country" => "France", "continent" => "Europe"),
 "GF" => array("country" => "French Guiana", "continent" => "South America"),
 "PF" => array("country" => "French Polynesia", "continent" => "Oceania"),
 "TF" => array("country" => "French Southern Territories", "continent" => "Antarctica"),
 "GA" => array("country" => "Gabon", "continent" => "Africa"),
 "GM" => array("country" => "Gambia", "continent" => "Africa"),
 "GE" => array("country" => "Georgia", "continent" => "Asia"),
 "DE" => array("country" => "Germany", "continent" => "Europe"),
 "GH" => array("country" => "Ghana", "continent" => "Africa"),
 "GI" => array("country" => "Gibraltar", "continent" => "Europe"),
 "GR" => array("country" => "Greece", "continent" => "Europe"),
 "GL" => array("country" => "Greenland", "continent" => "North America"),
 "GD" => array("country" => "Grenada", "continent" => "North America"),
 "GP" => array("country" => "Guadeloupe", "continent" => "North America"),
 "GU" => array("country" => "Guam", "continent" => "Oceania"),
 "GT" => array("country" => "Guatemala", "continent" => "North America"),
 "GG" => array("country" => "Guernsey", "continent" => "Europe"),
 "GN" => array("country" => "Guinea", "continent" => "Africa"),
 "GW" => array("country" => "Guinea-bissau", "continent" => "Africa"),
 "GY" => array("country" => "Guyana", "continent" => "South America"),
 "HT" => array("country" => "Haiti", "continent" => "North America"),
 "HM" => array("country" => "Heard Island and Mcdonald Islands", "continent" => "Antarctica"),
 "VA" => array("country" => "Holy See (Vatican City State)", "continent" => "Europe"),
 "HN" => array("country" => "Honduras", "continent" => "North America"),
 "HK" => array("country" => "Hong Kong", "continent" => "Asia"),
 "HU" => array("country" => "Hungary", "continent" => "Europe"),
 "IS" => array("country" => "Iceland", "continent" => "Europe"),
 "IN" => array("country" => "India", "continent" => "Asia"),
 "ID" => array("country" => "Indonesia", "continent" => "Asia"),
 "IR" => array("country" => "Iran", "continent" => "Asia"),
 "IQ" => array("country" => "Iraq", "continent" => "Asia"),
 "IE" => array("country" => "Ireland", "continent" => "Europe"),
 "IM" => array("country" => "Isle of Man", "continent" => "Europe"),
 "IL" => array("country" => "Israel", "continent" => "Asia"),
 "IT" => array("country" => "Italy", "continent" => "Europe"),
 "JM" => array("country" => "Jamaica", "continent" => "North America"),
 "JP" => array("country" => "Japan", "continent" => "Asia"),
 "JE" => array("country" => "Jersey", "continent" => "Europe"),
 "JO" => array("country" => "Jordan", "continent" => "Asia"),
 "KZ" => array("country" => "Kazakhstan", "continent" => "Asia"),
 "KE" => array("country" => "Kenya", "continent" => "Africa"),
 "KI" => array("country" => "Kiribati", "continent" => "Oceania"),
 "KP" => array("country" => "Democratic People's Republic of Korea", "continent" => "Asia"),
 "KR" => array("country" => "Republic of Korea", "continent" => "Asia"),
 "KW" => array("country" => "Kuwait", "continent" => "Asia"),
 "KG" => array("country" => "Kyrgyzstan", "continent" => "Asia"),
 "LA" => array("country" => "Lao People's Democratic Republic", "continent" => "Asia"),
 "LV" => array("country" => "Latvia", "continent" => "Europe"),
 "LB" => array("country" => "Lebanon", "continent" => "Asia"),
 "LS" => array("country" => "Lesotho", "continent" => "Africa"),
 "LR" => array("country" => "Liberia", "continent" => "Africa"),
 "LY" => array("country" => "Libya", "continent" => "Africa"),
 "LI" => array("country" => "Liechtenstein", "continent" => "Europe"),
 "LT" => array("country" => "Lithuania", "continent" => "Europe"),
 "LU" => array("country" => "Luxembourg", "continent" => "Europe"),
 "MO" => array("country" => "Macao", "continent" => "Asia"),
 "MK" => array("country" => "Macedonia", "continent" => "Europe"),
 "MG" => array("country" => "Madagascar", "continent" => "Africa"),
 "MW" => array("country" => "Malawi", "continent" => "Africa"),
 "MY" => array("country" => "Malaysia", "continent" => "Asia"),
 "MV" => array("country" => "Maldives", "continent" => "Asia"),
 "ML" => array("country" => "Mali", "continent" => "Africa"),
 "MT" => array("country" => "Malta", "continent" => "Europe"),
 "MH" => array("country" => "Marshall Islands", "continent" => "Oceania"),
 "MQ" => array("country" => "Martinique", "continent" => "North America"),
 "MR" => array("country" => "Mauritania", "continent" => "Africa"),
 "MU" => array("country" => "Mauritius", "continent" => "Africa"),
 "YT" => array("country" => "Mayotte", "continent" => "Africa"),
 "MX" => array("country" => "Mexico", "continent" => "North America"),
 "FM" => array("country" => "Micronesia", "continent" => "Oceania"),
 "MD" => array("country" => "Moldova", "continent" => "Europe"),
 "MC" => array("country" => "Monaco", "continent" => "Europe"),
 "MN" => array("country" => "Mongolia", "continent" => "Asia"),
 "ME" => array("country" => "Montenegro", "continent" => "Europe"),
 "MS" => array("country" => "Montserrat", "continent" => "North America"),
 "MA" => array("country" => "Morocco", "continent" => "Africa"),
 "MZ" => array("country" => "Mozambique", "continent" => "Africa"),
 "MM" => array("country" => "Myanmar", "continent" => "Asia"),
 "NA" => array("country" => "Namibia", "continent" => "Africa"),
 "NR" => array("country" => "Nauru", "continent" => "Oceania"),
 "NP" => array("country" => "Nepal", "continent" => "Asia"),
 "NL" => array("country" => "Netherlands", "continent" => "Europe"),
 "AN" => array("country" => "Netherlands Antilles", "continent" => "North America"),
 "NC" => array("country" => "New Caledonia", "continent" => "Oceania"),
 "NZ" => array("country" => "New Zealand", "continent" => "Oceania"),
 "NI" => array("country" => "Nicaragua", "continent" => "North America"),
 "NE" => array("country" => "Niger", "continent" => "Africa"),
 "NG" => array("country" => "Nigeria", "continent" => "Africa"),
 "NU" => array("country" => "Niue", "continent" => "Oceania"),
 "NF" => array("country" => "Norfolk Island", "continent" => "Oceania"),
 "MP" => array("country" => "Northern Mariana Islands", "continent" => "Oceania"),
 "NO" => array("country" => "Norway", "continent" => "Europe"),
 "OM" => array("country" => "Oman", "continent" => "Asia"),
 "PK" => array("country" => "Pakistan", "continent" => "Asia"),
 "PW" => array("country" => "Palau", "continent" => "Oceania"),
 "PS" => array("country" => "Palestinia", "continent" => "Asia"),
 "PA" => array("country" => "Panama", "continent" => "North America"),
 "PG" => array("country" => "Papua New Guinea", "continent" => "Oceania"),
 "PY" => array("country" => "Paraguay", "continent" => "South America"),
 "PE" => array("country" => "Peru", "continent" => "South America"),
 "PH" => array("country" => "Philippines", "continent" => "Asia"),
 "PN" => array("country" => "Pitcairn", "continent" => "Oceania"),
 "PL" => array("country" => "Poland", "continent" => "Europe"),
 "PT" => array("country" => "Portugal", "continent" => "Europe"),
 "PR" => array("country" => "Puerto Rico", "continent" => "North America"),
 "QA" => array("country" => "Qatar", "continent" => "Asia"),
 "RE" => array("country" => "Reunion", "continent" => "Africa"),
 "RO" => array("country" => "Romania", "continent" => "Europe"),
 "RU" => array("country" => "Russian Federation", "continent" => "Europe"),
 "RW" => array("country" => "Rwanda", "continent" => "Africa"),
 "SH" => array("country" => "Saint Helena", "continent" => "Africa"),
 "KN" => array("country" => "Saint Kitts and Nevis", "continent" => "North America"),
 "LC" => array("country" => "Saint Lucia", "continent" => "North America"),
 "PM" => array("country" => "Saint Pierre and Miquelon", "continent" => "North America"),
 "VC" => array("country" => "Saint Vincent and The Grenadines", "continent" => "North America"),
 "WS" => array("country" => "Samoa", "continent" => "Oceania"),
 "SM" => array("country" => "San Marino", "continent" => "Europe"),
 "ST" => array("country" => "Sao Tome and Principe", "continent" => "Africa"),
 "SA" => array("country" => "Saudi Arabia", "continent" => "Asia"),
 "SN" => array("country" => "Senegal", "continent" => "Africa"),
 "RS" => array("country" => "Serbia", "continent" => "Europe"),
 "SC" => array("country" => "Seychelles", "continent" => "Africa"),
 "SL" => array("country" => "Sierra Leone", "continent" => "Africa"),
 "SG" => array("country" => "Singapore", "continent" => "Asia"),
 "SK" => array("country" => "Slovakia", "continent" => "Europe"),
 "SI" => array("country" => "Slovenia", "continent" => "Europe"),
 "SB" => array("country" => "Solomon Islands", "continent" => "Oceania"),
 "SO" => array("country" => "Somalia", "continent" => "Africa"),
 "ZA" => array("country" => "South Africa", "continent" => "Africa"),
 "GS" => array("country" => "South Georgia and The South Sandwich Islands", "continent" => "Antarctica"),
 "ES" => array("country" => "Spain", "continent" => "Europe"),
 "LK" => array("country" => "Sri Lanka", "continent" => "Asia"),
 "SD" => array("country" => "Sudan", "continent" => "Africa"),
 "SR" => array("country" => "Suriname", "continent" => "South America"),
 "SJ" => array("country" => "Svalbard and Jan Mayen", "continent" => "Europe"),
 "SZ" => array("country" => "Swaziland", "continent" => "Africa"),
 "SE" => array("country" => "Sweden", "continent" => "Europe"),
 "CH" => array("country" => "Switzerland", "continent" => "Europe"),
 "SY" => array("country" => "Syrian Arab Republic", "continent" => "Asia"),
 "TW" => array("country" => "Taiwan, Province of China", "continent" => "Asia"),
 "TJ" => array("country" => "Tajikistan", "continent" => "Asia"),
 "TZ" => array("country" => "Tanzania, United Republic of", "continent" => "Africa"),
 "TH" => array("country" => "Thailand", "continent" => "Asia"),
 "TL" => array("country" => "Timor-leste", "continent" => "Asia"),
 "TG" => array("country" => "Togo", "continent" => "Africa"),
 "TK" => array("country" => "Tokelau", "continent" => "Oceania"),
 "TO" => array("country" => "Tonga", "continent" => "Oceania"),
 "TT" => array("country" => "Trinidad and Tobago", "continent" => "North America"),
 "TN" => array("country" => "Tunisia", "continent" => "Africa"),
 "TR" => array("country" => "Turkey", "continent" => "Asia"),
 "TM" => array("country" => "Turkmenistan", "continent" => "Asia"),
 "TC" => array("country" => "Turks and Caicos Islands", "continent" => "North America"),
 "TV" => array("country" => "Tuvalu", "continent" => "Oceania"),
 "UG" => array("country" => "Uganda", "continent" => "Africa"),
 "UA" => array("country" => "Ukraine", "continent" => "Europe"),
 "AE" => array("country" => "United Arab Emirates", "continent" => "Asia"),
 "GB" => array("country" => "United Kingdom", "continent" => "Europe"),
 "US" => array("country" => "United States", "continent" => "North America"),
 "UM" => array("country" => "United States Minor Outlying Islands", "continent" => "Oceania"),
 "UY" => array("country" => "Uruguay", "continent" => "South America"),
 "UZ" => array("country" => "Uzbekistan", "continent" => "Asia"),
 "VU" => array("country" => "Vanuatu", "continent" => "Oceania"),
 "VE" => array("country" => "Venezuela", "continent" => "South America"),
 "VN" => array("country" => "Viet Nam", "continent" => "Asia"),
 "VG" => array("country" => "Virgin Islands, British", "continent" => "North America"),
 "VI" => array("country" => "Virgin Islands, U.S.", "continent" => "North America"),
 "WF" => array("country" => "Wallis and Futuna", "continent" => "Oceania"),
 "EH" => array("country" => "Western Sahara", "continent" => "Africa"),
 "YE" => array("country" => "Yemen", "continent" => "Asia"),
 "ZM" => array("country" => "Zambia", "continent" => "Africa"),
 "ZW" => array("country" => "Zimbabwe", "continent" => "Africa")
);



Also you can create Dropdown of country...
Following code will generate Selectbox(Dropdown) :-

<select>
 <option>Choose country:</option>
 <?php
 foreach($countries as $country_code => $arr)
 {
  echo '<option value="'.$country_code.'">'.$arr['country'].'('. $arr['continent'] .')'.'</option>';
 }
 ?>
</select>

WordPress Include Page or Post within another Page or Post via Templates

Add this function to theme's functions.php.
And then make call in ones page.php, single.php, home.php, index.php or custom template file.
You can include on page within another.

//Add this function in theme's functions.php
function show_page($path)
{
    $page = get_page_by_path($path);
    $content = apply_filters('the_content', $page->post_content);
    echo $content;
}
//Call function from your page/post/template file
<?php  show_page('about'); // Display the content of the "About" page. ?>
<?php  show_page('category/postname'); // Display content of a Post page. ?>

Build a dynamic calendar with PHP

Here you can build a dynamic Calendar with PHP,

Following function will generate table structure of calender...

function build_calendar($month,$year)
{
 // Create array containing abbreviations of days of week.
 $daysOfWeek = array('S','M','T','W','T','F','S');
 // What is the first day of the month in question?
 $firstDayOfMonth = mktime(0,0,0,$month,1,$year);
 // How many days does this month contain?
 $numberDays = date('t',$firstDayOfMonth);
 // Retrieve some information about the first day of the
 // month in question.
 $dateComponents = getdate($firstDayOfMonth);
 // What is the name of the month in question?
 $monthName = $dateComponents['month'];
 // What is the index value (0-6) of the first day of the
 // month in question.
 $dayOfWeek = $dateComponents['wday'];
 // Create the table tag opener and day headers
 $calendar = "<table class='calendar'>";
 $calendar .= "<caption>$monthName $year</caption>";
 $calendar .= "<tr>";
 // Create the calendar headers
 foreach($daysOfWeek as $day) {
  $calendar .= "<th class='header'>$day</th>";
 }
 // Create the rest of the calendar
 // Initiate the day counter, starting with the 1st.
 $currentDay = 1;
 $calendar .= "</tr><tr>";
 // The variable $dayOfWeek is used to
 // ensure that the calendar
 // display consists of exactly 7 columns.
 if ($dayOfWeek > 0) {
  $calendar .= "<td colspan='$dayOfWeek'>&nbsp;</td>";
 }
 $month = str_pad($month, 2, "0", STR_PAD_LEFT);
 while ($currentDay <= $numberDays)
 {
  // Seventh column (Saturday) reached. Start a new row.
  if ($dayOfWeek == 7) {
   $dayOfWeek = 0;
   $calendar .= "</tr><tr>";
  }
  $currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
  $date = "$year-$month-$currentDayRel";
  if($date == date('Y-m-d'))
  {
   $calendar .= "<td class='day' rel='$date' style='color:red;'><b><em>$currentDay</em></b></td>";
  } else {
   $calendar .= "<td class='day' rel='$date'>$currentDay</td>";
  }
  // Increment counters
  $currentDay++;
  $dayOfWeek++;
 }
 // Complete the row of the last week in month, if necessary
 if ($dayOfWeek != 7) {
  $remainingDays = 7 - $dayOfWeek;
  $calendar .= "<td colspan='$remainingDays'>&nbsp;</td>";
 }
 $calendar .= "</tr>";
 $calendar .= "</table>";
 return $calendar;
}


Simple uses:-

/* For July 2023 */
echo build_calendar(7,2013);
/* For August 2013*/
echo build_calendar(8,2013);


Also, You can add Css to this calender. Eg:-

</style>
    table.calendar {}
    .calendar tr {}
    .calendar td {}
</style>

23 December 2013

Unzip files with PHP

With below function you can extract "zip" files to specified directory...

<?php
/**
* Unzip files
* @param $file Zip file
* @param $destination Directory Path
*/
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';
    return true;
}
?>

How to Enable Shortcodes in Text Widgets

To Enable Shortcodes In Text Widgets,

Add the following code into your theme’s function.php

<?php

// This code enables Shortcodes in WordPress Text Widget
add_filter('widget_text', 'do_shortcode');

?>

List All Hooked Functions

WordPress hooks are very useful because they allow you to “surcharge” an existing WP function with your own code. But when things goes wrong, it should be useful to be able to list all hooked WordPress functions. Here is the code to it.

The first thing to do is to paste the following function in your functions.php file:

function list_hooked_functions($tag=false)
{
    global $wp_filter;
    if ($tag) {
        $hook[$tag]=$wp_filter[$tag];
        if (!is_array($hook[$tag])) {
            trigger_error("Nothing found for '$tag' hook", E_USER_WARNING);
            return;
        }
    } else {
        $hook=$wp_filter;
        ksort($hook);
    }
    echo '<pre>';
    foreach($hook as $tag => $priority)
    {
        echo "<br />&gt;&gt;&gt;&gt;&gt;t<strong>$tag</strong><br />";
        ksort($priority);
        foreach($priority as $priority => $function){
            echo $priority;
            foreach($function as $name => $properties) {
                echo "t$name<br />";
            }
        }
    }
    echo '</pre>';
    return;
}


Once done, simply call the list_hooked_functions() function to print on the screen all hooked WordPress functions.
Please note that this function is for debugging purposes only.

list_hooked_functions();

Set permalink settings from functions.php

Use this code to change the permalink settings from your functions.php file.

The .htaccess mod_rewrite it is still necessary.

<?php
// set permalink
function set_permalink()
{
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%postname%/');
}
add_action('init', 'set_permalink');
?>

21 December 2013

Schedule cron jobs with WordPress

Add This code in theme's function.php file...
<?php
add_action('my_hourly_event', 'do_this_hourly');
function my_activation() {
    if ( !wp_next_scheduled( 'my_hourly_event' ) ) {
         wp_schedule_event(time(), 'hourly', 'my_hourly_event');
    }
}
add_action('wp', 'my_activation');
function do_this_hourly() {
    // do something every hour
}
?>