Breaking Strings into Arrays with explode()
PHP/MySQL
Last Updated: 2007-02-25 07:51:12
Last Updated: 2007-02-25 07:51:12
STEP 1: The explode() function
There are many times when you may need to separate a string into separate strings to better manipulate the information a string provides. To do this, you can use the explode() function.
The explode() function requires two arguments; the delimiter string used to break up the source string and the source string itself. There is an optional third argument that tells explode() the maximum number of array pieces it can create.
In this example we have a variable that represents a full name of a person. We want to break this variable into two variables for first and last name.
Here is the code.
<? php
$name = "Joe Fitzgerald";
$nameArray = explode(" ", $name);
$firstName = $nameArray[0]; // sets this variable to "Joe"
$lastName = $nameArray[1]; // sets this variable to "Fitzgerald"
?>
So lets review. What we just did is feed the explode() function the variable $name, which was set to "Joe Fitzgerald. Then we told explode() to separate $name into an array when it comes across the delimiter of " " (empty space).
Now when we set the delimiter, we can set it to anything we want to set it at. The delimiter argument will accept more than one character, which will combine them all into one delimiter for separation.
The explode() function requires two arguments; the delimiter string used to break up the source string and the source string itself. There is an optional third argument that tells explode() the maximum number of array pieces it can create.
In this example we have a variable that represents a full name of a person. We want to break this variable into two variables for first and last name.
Here is the code.
<? php
$name = "Joe Fitzgerald";
$nameArray = explode(" ", $name);
$firstName = $nameArray[0]; // sets this variable to "Joe"
$lastName = $nameArray[1]; // sets this variable to "Fitzgerald"
?>
So lets review. What we just did is feed the explode() function the variable $name, which was set to "Joe Fitzgerald. Then we told explode() to separate $name into an array when it comes across the delimiter of " " (empty space).
Now when we set the delimiter, we can set it to anything we want to set it at. The delimiter argument will accept more than one character, which will combine them all into one delimiter for separation.
This is the JoeLucky39 tutorial section. Feel free to use whatever information you find here on your own site(s). Know that not all tutorials are ideal for all sites, so feel free to modify the information contained in this tutorail section as best suits your web site.
At JoeLucky39 you can find tutorials on Comic Art, PHP/MySQL, and more are added all the time. So, visit often and feel free to contact Joe if you think that his tutorials need to be changed or updated.
Joe believes in the open source movement and is happy to share his knowledge with anyone who asks. All Joe asks is, if you use Joe's open source tutorials, you share your knowledge as well.
Thank you and I hope you find what you are looking for. If not feel free to contact me and ask.
