Visit the Official PHP Website
By James N Hitz
The preg_split() function
I like using this function very much. Using this function, you can automatically create an array by splitting a string of text at pre-defined locations indicated using given delimeters. Th syntax for using preg_split() is:
$createdArray = preg_split("delimeter","OriginalString");
An example should be in order. Let's say we have the following employee record:
$employee = JB121|John|Brown|Information Technology|12-Dec-1992;
This (supposedly) is an extract from a database showing the fields: Employee_ID |First_Name |Last_Name |Department |Date_Hired.
To convert this into an array can be helpful at times. To do so we use preg_split as follows:
$emp = preg_split("/\|/", $employee);
Notice here that the pipe symbol (|) is escaped (\|).
OK. So that's it. For starters regular expressions can be very intimidating but with time - as you your PHP-ing skills evolve - you will realize that regular expressions are actually a straight-forward affair. Finito. But before winding up this lesson, let's make a super-example to encompass most of what we have learnt in this and the preceeding lesson.