Concept
How can PHP split strings into tokens using explode() and preg_split()?
ComputerScienceOne / Tokenizing
"Tokenizing is the process of splitting up a string along some delimiter. For example, the comma-delimited string, \"Smith,Joe,12345678,1985-09-08\" contains four pieces of data delimited by a comma. Our aim is to split this string up into four separate strings so that we can process each one. PHP provides several functions to do this, including explode() and preg_split(). The simpler one, explode(), takes two arguments: the first is a string delimiter and the second is the string to be processed. It then returns an array of strings.\n\n$data = \"Smith,Joe,12345678,1985-09-08\";\n\n$tokens = explode(\",\", $data);\n\n// tokens is [ \"Smith\", \"Joe\", \"12345678\", \"1985-09-08\" ]\n\n$dateTokens = explode(\"-\", $tokens[3]);\n\n// dateTokens is now [ \"1985\", \"09\", \"08\" ]\n\nThe more sophisticated preg_split() also takes two arguments, but instead of a simple delimiter, it uses a regular expression: a sequence of characters that define a search pattern in which special characters can be used to define complex patterns. The “preg” stands for Perl Compatible Regular Expression. For example, the complex expression ^[+-]?(\\d+(\\.\\d+)?|\\.\\d+)([eE][+-]?\\d+)?$ will match any valid numerical value including scientific notation. Regular expressions are not covered in depth here, but they can be used to split a string along any and all whitespace:\n\n$s = \"Alpha Beta \\t Gamma\n\nDelta \\t\n\nEpsilon\";\n\n$tokens = preg_split(\"/[\\s]+/\", $s);\n\n// tokens is now [ \"Alpha\", \"Beta\", \"Gamma\", \"Delta\", \"Epsilon\" ]"
Related Ideas
- How can you tokenize a String in Java?ComputerScienceOne · Tokenizing
- How can strtok() split a string into tokens?ComputerScienceOne · Tokenizing
- Why is strtok() not reentrant, and what alternative does C provide?ComputerScienceOne · Tokenizing
- How can you define arrays of strings in PHP?ComputerScienceOne · Arrays of Strings
- What are PHP strings and how should they be treated?ComputerScienceOne · Basics
- How can you determine the length of a PHP string?ComputerScienceOne · String Functions