Implode and Explode in PHP

We can store multiple value in single variable in two ways.
Either using Array, or comma separated values.

Comma separated: 
$name = 'Alex,Ana,BOB';
Array:  
$name =  array(
    [0] => 'BOB',
    [1] => 'Alex',
    [2] =>  'Ana'
);

To convert array to comma separated value and reverse
we required two function

implode : 
Syntax:  string implode ( string $glue , array $pieces )

$array = array(
    [0] => 'lastname',
    [1] => 'email',
    [2] => 'phone'
);
$comma_separated = implode(",", $array);

result :   will be "lastname,email,phone".
explode : 
Syntax:  array explode (string $delimiter,string $comma_string)

$input2 = "hello,there";
$result = var_dump( explode( ',', $input2 ) );

result : array( 
    [0] => 'hello',
    [1] => 'there'
);

Thanks ……!

cpanel xmlapi create email accounts through your code

Came across with something interesting and felt like sharing it..
I was doing this networking website for one of my client and one of the requirement was that whenever a new user registers on the website, his/her email id username@networkingwebsite.com should be created automatically.
I started R&D if that’s possible in the first place. Google is my friend and i came across with cpanel api.

cpanel is a linux hosting application and thus wont support .net, so its not for .net developers. I am yet to find something equivalent on any windows hosting application

What is it

If you have a hosting account which has cpanel interface (most of the linux hosting providers have cpanel only i guess), you can write your own php code to access your hosting service and perform actions like adding/deleting email accounts, ftp accounts, adding sub-domains etc..

Through the hard-to-navigate-through documentation i found my way to xmlapi class which provides helper functions to make things easier for you. You can download the xmlapi class along with examples from github

Changes

The examples provided with the download uses WHM root user’s credentials (lets not go into details, coz i have no clue at all 🙂 ). I’ll take my example, i have a linux hosting account(not reseller) and i have cpanel access, obviously i have limited autorization to perform actions on the server, i dont have administrative priviledges equal to the root user of the server. So i had to change few things to suit my needs.

An example code to create email accounts on your server would be:


<?php 
include_once "xmlapi.php" ;/*the file donwloaded from github*/

$ip = 'your.servers.ip.address';
$root_pass = 'your_cpanels_login_password';

$account = "your_cpanels_login_name";

$email_user = "wdw";
$email_password = "pass123";
$email_domain = "your_domain.com";/*this will create 'wdw@your_domain.com'*/
$email_quota = '10';

$xmlapi = new xmlapi($ip);
$xmlapi->set_port(2083);/* the ssl port for cpanel */
$xmlapi->password_auth($account,$root_pass);
$xmlapi->set_output('json');

$xmlapi->set_debug(1);
print $xmlapi->api1_query($account, "Email", "addpop", array($email_user, $email_password, $email_quota, $email_domain) );

?>

So if your login creadentials are correct the above code will create wdw@your_domain.com which you can see listed under all email accounts when you login to your cpanel.

So yea.. that’s it.. I found it pretty cool, can definitely think of using the api in many ways.