Thursday, June 27, 2013

PHP date validation

Date Validation yyyy-mm-dd
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


$date = '2013-06-27';

function isValidDate($date)
{
 if(preg_match("/^(\d{4})-(\d{2})-(\d{2})$/", $date, $matches))
 {
  if(checkdate($matches[2], $matches[3], $matches[1]))
  {
   return true;
  }
 }
}

if(isValidDate($date)){
 echo 'valid date';
} else
{
 echo 'invalid date';
}

?>










Date Validation dd-mm-yyyy
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

$date = '27-06-2013';

function isValidDate($date)
{
 if(preg_match("/^(\d{2})-(\d{2})-(\d{4})$/", $date, $matches))
 {

  if(checkdate($matches[2], $matches[1], $matches[3]))
  {
   return true;
  }
 }
}

if(isValidDate($date))
{
 echo 'valid date';
} else
{
 echo 'invalid date';
}


?>








Date Validation mm-dd-yyyy

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


$date = '06-27-2013';

function isValidDate($date)
{
 if(preg_match("/^(\d{2})-(\d{2})-(\d{4})$/", $date, $matches))
{
  if(checkdate($matches[1], $matches[2], $matches[3]))
  {
   return true;
  }
 }
}

if(isValidDate($date)){
 echo 'valid date';
} else
{
 echo 'invalid date';
}

?>









Date Validation dd-mmm-yyyy
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


$date = '27-Jun-2013';

function isValidDate($date)
{
 if(preg_match("/^(\d{2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{4})$/", $date, $matches)) 
 {
  $month = array('Jan'=>'01'
         ,'Feb'=>'02'
         ,'Mar'=>'03'
         ,'Apr'=>'04'
         ,'May'=>'05'
         ,'Jun'=>'06'
         ,'Jul'=>'07'
         ,'Aug'=>'08'
         ,'Sep'=>'09'
         ,'Oct'=>'10'
         ,'Nov'=>'11'
         ,'Dec'=>'12'
        );

  if(checkdate($month[$matches[2]],$matches[1],$matches[3]))
  {
   return true;
  }
 }
}

if(isValidDate($date))
{
 echo 'valid date';
} else
{
 echo 'invalid date';
}

?>

PHP function to calculate percentage

This post shows you how to calculate a percentage using a division and multiplying its result by 100. This is the simpliest way to calculate a percentage in PHP
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


function percentage($x, $y, $precision) 
{

        // $precision define digit after the decimal point

 $result = round( ($x / $y) * 100, $precision );
 
 return $result;
}


echo percentage(2, 3, 2); // This will print 66.67

echo percentage(2, 3, 0); // This will print 67

?>

Friday, June 21, 2013

Replace characters with icons emoticons php

Learn to use PHP to change character sequences into emoticons or icons by using the str_replace function. You will want to alter the string data as it comes out of the MySQL database for display, not when the data is being inserted into the database.

# USAGE EXAMPLE 

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


$db_string = "Show me some <3 right now. It is raining today [umbr], that gives me peace [peace].";

echo $db_string;
echo "
"; $chars = array("<3", "[peace]", "[umbr]"); $icons = array("❤", "☮", "☂"); $new_str = str_replace($chars,$icons,$db_string); echo $new_str; ?>

Get Favicon From other Website Using PHP


Learn to get and render favicon.ico files dynamically from other servers in your PHP scripts. You can look for files and also process the DOM structure of external pages on the web. I recommend saving the favicon image file to your server converting it to a JPG or PNG file if it will be a frequently viewed image.

# USAGE EXAMPLE 

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/



function getfavicon($url){
    $favicon = '';
    $html = file_get_contents($url);
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $links = $dom->getElementsByTagName('link');
    for ($i = 0; $i < $links->length; $i++){
        $link = $links->item($i);
        if($link->getAttribute('rel') == 'icon'){
            $favicon = $link->getAttribute('href');
 }
    }
    return $favicon;
}


$website = "http://4evertutorials.blogspot.com/"; # replace with your url
$favicon = getfavicon($website);
echo $favicon;

?>

Render XML file for Flash Photo Gallery with php

Learn how to render dynamic XML files for Flash(or any other) Photo Gallery applications using PHP loops to show the most current data at all times.


This script would be named something like galleryXML.php

This file reads a directory on its own to render the XML file, all you have to do it point it at a folder of images no matter how many are in it, and it will produce nice clean XML nodes for all of the images.




<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


# set the content type to xml
header("Content-Type: text/xml");

# Initialize the xmlOutput variable
$xmlBody = '';

# Specify Directory where images are 
$dir = "images/gallery/"; 

# Start XMLBody output
$xmlBody .= ""; 

# open specified directory using opendir() the function
$dirHandle = opendir($dir); 

# Create incremental counter variable
$i = 0;
while ($file = readdir($dirHandle)) 
{ 
      
      # if file is not a folder and if file name contains the string .jpg  

      if(!is_dir($file) && strpos($file, '.jpg'))
      {
        # increment $i by one each pass in the loop
         $i++; 
         $xmlBody .= '

    ' . $i . '
    ' . $dir . '' . $file . '
';
      } # close the if statement

} # End while loop


# close the open directory
closedir($dirHandle); 

$xmlBody .= "";

# output the gallery data as XML file for Flash Photo Gallery
echo $xmlBody; 


?>

Cookies and Session in PHP

Cookies and Session are use in terms of data storage in programming language.

But how ?
What is the preferable?,
When use session?, 
When use cookie?, 

there are some question which arise in the mind at the time of of store data temp. as well as permanent.

There are differences between the two that will make each favorable in their own circumstance.

Cookies can be set to a long lifespan, which means that data stored in a cookie can be stored for months if not years. Cookies, having their data stored on the client machine, work smoothly when you have a cluster of web servers, whereas sessions are stored on the server, meaning if one of your web servers handles the first request, the other web servers in your cluster will not have the stored information.

Sessions are stored on the server, which means clients do not have access to the information you store about them – this is particularly important if you store shopping baskets or other information you do not want your visitors to be able to edit by hand by hacking their cookies. Session data, being stored on your server, does not need to be transmitted with each page; clients just need to send an ID and the data is loaded from the local file. Finally, sessions can be any size you want because they are held on your server, whereas many web browsers have a limit on how big cookies can be to stop rogue web sites chewing up gigabytes of data with meaningless cookie information.

So, as you can see, each have their own advantages, but at the end of the day it usually comes down to one choice: 

Do you want your data to work when you visitor comes back the next day?

If so, then your only choice is cookies – if you have any particularly sensitive information, your best bet is to store it in a database, then use the cookie to store an ID number to reference the data. If you do not need semi-permanent data, then sessions are generally preferred, as they are a little easier to use, do not require their data to be sent in entirety with each page, and are also cleaned up as soon as your visitor closes their web browser.

 

© 2014 4everTutorials. All rights resevered.

Back To Top