PHP String Split

Richard CummingsQuick Solutions, Technologies/SolutionsLeave a Comment

In this post, I am going to tell you how to do a PHP string split. While there use to be a PHP command called “PHP split“, it is no longer being supported so we will teach you how to do a PHP string split using the PHP explode command.

How To Do a PHP String Split Using Explode

Let us illustrate how to do a PHP string split by example. I recently had a list of employees and job titles that was structured like this:

Jim Brown, Star Running Back
Aaron Rodgers, Superbowl Winning Quarterback
Tiger Woods, Golfer and Womanizer

I do not know who set up the list originally but it was not structured as we needed. We wanted to add the information to a MySQL database with the full name in one column and the profession in the other. In other words, we had to split the one PHP string into two that equaled the name and profession.

So, for example, we want to turn the variable $nameprofession=”Jim Brown, Star Running Back” into $name=”Jim Brown” and $profession=”Star Running Back”.

So, without further ado, let us see how we do this PHP String split.

Here is the PHP file that accomplished this and I will discuss it below:

<?
// how to do a string split using explode

$nameprofession="Jim Brown, Star Running Back";
$explodeit = explode(", ", $nameprofession);
$name=$explodeit[0];
$profession=$explodeit[1];
echo $name."<br />";
echo $profession;

?>

You can copy and paste this file into your favorite text editor (UltraEdit is mine), name it with a .php extension, and upload it to your web server to see it in action.

PHP String Splitting With Explode: Explained

Ok, so let’s take a look at what is happening in the file above.

We are exploding the variable $nameprofession using a comma and space as our delimiter and giving the result a name of $explodeit, which provides us an array as the result. And, remember, arrays begin with “0” not “1”.

Thus, $explodeit[0] equals the name of the person (which I then named $name for clarity) and $explodeit[1] equals the profession. NOTE: The comma and space get removed.

The last couple of ECHO statements simply print out the result.

PHP String Split: Conclusion

For me, PHP String Splits are best done using the PHP explode command.

The most important element in doing an explode command is determining what you will explode. In this case, as you saw, we used comma space as the delimiter. In other cases, it may be a colon or semicolon or something of that nature.

So, how do you this with a whole list of names and not just one? How do you write all of this information to a MySQL database? That will be a discussion for another post. However, if you want to do some experimentation, you simply place the above commands in a php FOR or WHILE loop to change each entry and add it to the MySQL database on each run through.

Sometimes, it can be difficult to find solutions to what are seemingly simple PHP problems. I hope this post has helped you discover how to do a PHP String Split quickly.

If you want to be apprised of future PHP posts, simply subscribe to Richard Cummings below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.