Forgot Password / Register
Site Statistics
Total Members: 510
Total Tutorials: 242
Newsest User: Hp
Todays Unique Hits: 121
0 Users 1 Guests Online

Explode Function - The Basic's

The explode functions splits a PHP variable by a defined delimiter.
This can be very useful in many cases, to split information stored in a special format or to parse a data of birth like we will in this example.

Example 1: Basic example using Date Of Birth
1
2
3
4
5
6
7
<?php
$mydob = "01/04/1990"; // Setting the DOB variable
$dob = explode("/", $mydob); // Splits the data using the delimiter which in this case is a "/"
$day = $dob[0]; // The first in the series is the day of birth
$month = $dob[1]; // Second is the month
$year = $dob[2]; // Third is the year of birth
?>

The above splits the data at a '/' section this could be set as any thing such as : which will mean the new date of birth will be 01:04:1990

Example 2: Assigning to Variables
1
2
3
4
5
<?php
$data = "DanielXP:mYPaSS:daniel@somesite.com"; // The data we wish to split using the explode function. In this example this is a username, password and email. The delimiter being a ":"
list($username, $password, $email) = explode(":", $data); // This splits the data by the delimiter and the list functions assigns them to the defined variables.
echo "Welcome ".$username." - You email is ".$email;
?>


I hope this tutorial has been informative.
DanielXP
Author:
Views:
1978
Rating:
There are currently no comments for this tutorial.