Thursday, 2 March 2017

How to parse XML in PHP – simpleXML

How to parse XML in PHP – simpleXML

Many websites on internet like to share their feed in XML format so that other website owner can show that feed on his/her website / blog. In PHP there is a function called simpleXML can simplify the process of reading the feeds into something useful for your web pages.

If You have wordpress installed in your server you can also on Rss feed of your blog and share your news/ articles with other websites. this will increase your blog visibility and good for SEO also.
See tutorial: Read RSS feed of website (blog) using php


Here is sample xml file.

companydb.xml

<?xml version='1.0'?>
<companydb>
  <company>
        <name>IAMROHIT</name>
        <city>Dellhi</city>
        <phone>0000000</phone>
  </company>
    <company>
        <name>IAMROHIT</name>
        <city>Dellhi</city>
        <phone>0000000</phone>
  </company>
</companydb>


With simpleXML, it’s as easy reading the XML file and then accessing it’s contents by an easy to read object. Suppose we have our XML file above saved as a file called comopanydb.xml with all the company details in the same folder as our php file, we can read the whole xml feed by following php function.

 $companydb = simplexml_load_file('companydb.xml');

Now we have created file object, go next for accessing it’s content. like if you want to display the name of the all companies then use below code.

<?php
    $companydb = simplexml_load_file('companydb.xml');
    foreach ($companydb as $company) {
        echo $title=$company->name." - ".$company->city." - ".$company->phone."<br/>";      
  } 
?>

Above is the quick example for parsing xaml data in php, you can store you data in xml file and access with this method.

Wednesday, 1 March 2017

How to create custom component in cakephp

How to create custom component in cakephp



In this tutorial we are going to write our own hello world component (plugin) in CakePHP.

The component is a logic container in CakePHP (ex. plugin in WordPress). All just needed is, include it and utilize those functions in that component. If some CakePHP developers want share their programming logic or functionality with fellow CakePHP developers, they will write as a component and share it with their programming community.

Once your Cakephp installation, database connections and security salt setting everything done. We are now able write our first hello world CakePHP component.


Step 1:

I am going to create component in the name of muni i.e. MuniComponent

To create your CakePHP component all you needed is to create php file in the follwoing directory. That’s app/controller/component/MuniComponent.php

Every component class we are creating must extends base component class that’s in the CakePHP library folder (lib/Cake/Controller/Component.php). So first in the component import component class.

App::uses('Component', 'Controller');

Where Controller – Indicates folder name
Component — Indicates class name we are including in the file.


Now create Component class, and write hello() function that returns ‘hello world’ string.

<?php 

App::uses('Component', 'Controller');

class MuniComponent extends Component {

    // hello world function
public function hello() {
return 'hello world';
}
}

?>

That’s it you successfully created your first hello world compnont in CakePHP, so any one use your component in their application .i.e. controller.

Step 2:


Now we are going to use the componenet(MuniComponent) just we written now. So I am going use this component in my UsersController. Before use any component in a controller, we must include it in a controller.

public $components = array('Muni');

Now I am going use hello() function of the component in the Controller, which going to return ‘hello world’ string.

<?php

App::uses('Controller', 'Controller');

class UsersController extends Controller{

//include component you want to use
public $components = array('Muni');

public function index()
{
$this->layout = 'common';
//calling component function we written
$data = $this->Muni->hello();
$this->set( 'data', $data);
}
}
?>


I have added one more function in the MuniComponent, that’s passwordGenerator() function which going to return random password when we are going to call it.

// password generator function
public function passwordGenerator( $length = 10){
    $letters ="@#$%^&*()_-+<>':,.1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    return substr(str_shuffle($letters), 0, $length);
}

In controller

//calling password generator function of component
$pass = $this->Muni->passwordGenerator(20);
$this->set( 'pass', $pass);

Tuesday, 28 February 2017

Date Format Validation in PHP

How can you validate date format in php using Regex and without Regex using simple php date function. If you are working on any application where right data format is mandatory then you must have to apply right data format validation on server side so that you can easily calculate taken duration for any task.


Date Format Validation in PHP



Going to validate this format YYYY-MM-DD

Date Format Validation using Regex


function validateDate($data) {
if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date))
    {
        return true;
    }else{
        return false;
    }
}

var_dump(validateDate("2017-02-27")) // true
var_dump(validateDate("27-02-2017")) // false
var_dump(validateDate("2017-14-27")) // false



Date Format Validation using PHP Date object


function validateDate($date)
{
    $dt = DateTime::createFromFormat('Y-m-d', $date);
    return $dt && $dt->format('Y-m-d') === $date;
}
var_dump(validateDate("2017-02-27")) // true
var_dump(validateDate("27-02-2017")) // false
var_dump(validateDate("2017-14-27")) // false

HTML APIs: What They Are And How To Design A Good One

As JavaScript developers, we regularly forget that not everybody has a similar data as USA. It’s referred to as the curse of knowledge:...