| Welcome to zetaNetwork. We hope you enjoy your visit. You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as viewing zetaNetwork exclusive tutorials and articles, and access to our code snippets section. Registration is simple, fast, and completely free. Click Here To Register If you're already a member please log in to your account to access all of our features: |
| 3 Ways To Turn A String Into An Array | |
|---|---|
| Tweet Topic Started: May 18 2011, 04:07 AM (11,252 Views) | |
| Pro | May 18 2011, 04:07 AM Post #1 |
|
Underground Coding
![]()
|
3 Ways To Turn A String Into An Array Difficulty: Easy Time: 10 Minutes Today I'm going to be showing you 3 ways to turn a string into an array. By this I mean we will be taking a delimiter, such as a space character, and using that to split our string into an array. If you're familiar with the JavaScript split function you should know what I mean. 1. Explode The explode function is rather easy to use.It is also the fastest out of all 3 methods in this tutorial, if you don't need regular expressions that is. $delimiter is the delimiter that you will be using to split your string. Unlike preg_split, discussed below, explode can only use strings as delimiters, regular expressions are not valid syntax. $string is the string that you will be splitting. $limit is the maximum number of elements that will be in the returned array. If the limit is passed and it is positive everything past the limit will be added onto the last element in the returned array. If it is negative everything will be returned in the array except the last $limit elements. If it is zero then it is assumed to be 1. It is important to note that while the JavaScript implementation of split will return an array containing every character in a string when the specified delimiter is an empty string or "", explode will return false if an empty string is used as the delimiter. Also, if the delimiter is not found within the given string and a negative limit is specified then an empty array, an array with no elements, will be returned. In addition to that, if the delimiter is not found within the given string and a negative limit is not specified then the returned array will contain one element, the given string. 2. Str_Split The str_split function is similar to explode. However, instead of using a character as the delimiter, a character count is used. Also, you can not specify a limit. $string is the string to be split. $length is the length of each split. $length is optional, if $length is not specified it will be 1, meaning that string will be split at every character. If $length is specified then the string will be split at every $length character. If this is specified then it must be greater than zero otherwise you will get an error. If the specified limit is less than one an error will occur. Also, if the specified limit is greater than the number of characters in the specified string then an array only containing the string will be returned. 3. Preg_Split The preg_split function is also similar to explode, however, preg_split accepts a PCRE pattern instead of a string delimiter. $pattern is the pattern that will be used to split the specified string. Keep in mind that it is a PCRE pattern. $string is the string that will be split. $limit is similar to that of explode, however, if it is 0, -1, or null then that means no limit. $flags is used to return addition information about the split. Please note that if you do not want to set a limit but want to use a flag you can set $limit to 0, -1, or null. Multiple flags can be used as long as they are separated by a | or "or" boolean operator and are as follows:
Well that's pretty much it for this tutorial. Disappointing I know right? Well don't worry, more are coming soon. If you have any questions don't be afraid to ask them. |
|
Pro | zetaNetwork Instructor & Admin I Coded Most Of The Smexy Stuff You See ![]() PM Me Any Questions Need live support? Click here. | |
| Offline Profile | Quote ^ |
| 1 user reading this topic (1 Guest and 0 Anonymous) | |
| « Previous Topic · Tutorials · Next Topic » |











2:34 PM Jul 11