Php splits strings and outputs array character examples
The explode () function can be used to split character functions in php, but this function must have a regular pattern, for example, separating with | or other characters, in this way, we can directly use explode to divide the string into arrays and then use for traversal output. Here are several examples.
The explode () function separates strings into arrays.
Syntax
Explode (separator, string, limit)
Example 1
The Code is as follows: |
|
<? Php $ Test = '2014 372115,850965403 '; $ R = explode (",", $ test ); For ($ I = 0; $ I <sizeof ($ r); $ I ++) {Echo $ I. ".". $ r [$ I]. "";} ?> |
Output: 0.472347127 1.893372115 2.850965403
Example 2
The Code is as follows: |
|
<? Php $ A = "893372115,472369127, 850965403 "; $ B = explode (",", $ ); Foreach ($ B as $ bb) {Echo $ bb. ""; // print_r ($ B );} ?> |
Output: 893372115 472347127 850965403 PHP
Comma-separated string
Use the explode function to split a string into an array
The Code is as follows: |
|
<? Php $ Source = "hello1, hello2, hello3, hello4, hello5"; // separate strings by commas $ Hello = explode (',', $ source ); For ($ index = 0; $ index <count ($ hello); $ index ++ ){ Echo $ hello [$ index]; echo "</br> "; } ?> |
Split function for Character Segmentation
The Code is as follows: |
|
<? Php // The delimiter can be a diagonal line, a dot, or a horizontal line. $ Date = "04/30/1973 "; List ($ month, $ day, $ year) = split ('[/.-]', $ date ); Echo "Month: $ month; Day: $ day; Year: $ year <br/> n "; ?> |