SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

Breaking of strings to create array using split command

We can break strings to form arrays. We can break by using a pattern or by regular expression. This way we can break strings to create array of string. Here is the syntax.

array split (string pattern, string string [, int limit])

We can specify ( option )the limit of breaking required.


If we have a string with commas ( , ) between the words then we can break it to form arrays. For example here is a string with name of the months separated by commas. We will try to break this by using split command.


$months="Jan, Feb, Mar, Apr, May, Jun, July, Aug, Sep, Oct, Nov,Dec";
$mn=split(",",$months);
echo "$mn[2]<br>";
echo "$mn[3]<br>";


One of the main uses is creating a month date and year from the data entered by the users. Users will enter date in one input box in mm/dd/yy format. We will use this string and break it into three parts storing month, date and year in three different variables for further processing. Here is the code how this works.

We will get the value from the form and the date entered by the user is available in variable $mytime. Where $mytime="05/13/05";

$arr=split("/",$mytime); // splitting the array
$mm=$arr[0]; // first element of the array is month
$dd=$arr[1]; // second element is date
$yy=$arr[2]; // third element is year


From this date data we can format our date field as per our requirement. Read how the user entered date value is validated by using checkdate function.

Read how split command is used to separate domain and user id part from an email address

Split function in ASP to create an array by breaking a string

Discuss this tutorial at forum


PHP String Functions
All String Functions in PHP
str_replace: How to replace a part of a string with another string
str_ireplace: Case in-sensitive search and replace using array of strings
strlen: How to find length of a string in PHP?
trim: Removing empty space from both sides of a string
strrev: Reversing a string by using strrev function in PHP
Adding two or more strings in PHP
stristr: Searching for a presence of a string inside another string
nl2br: Adding Line breaks inside a string in place of carriage returns
split: Breaking a string to form array using delimiters
substr: Collecting part of a string
substr_count: Counting occurrence of sub string in main string
str_repeat: Repeating a string number of times
strtolower(): function to change alphabets to Lower case
strtoupper(): function to change characters to upper case letters
function to collect part of a string with two landmarks
Random string generator with number and alphabets for password
strcasecmp(): Case insensitive string comparison
strcmp(): Case sensitive string comparison
str_pad(): String pad with specified length
md5 hash of a string: encryption of a string
Separating domain and userid part from an email address using split
htmlspecialchars: Printing html special chars to the page
strip_tags: Removing html tags within a string
ucwords: Removing html tags within a string



 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorials
String Functions
PHP Sections
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.