This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
uphp:functions:explode [2017/02/28 13:35] jeff |
uphp:functions:explode [2021/09/13 05:57] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
======explode====== | ======explode====== | ||
+ | |||
< | < | ||
- | Turn a string into an array | + | < |
====Description==== | ====Description==== | ||
- | <well size=" | + | |
- | <span style=" | + | <well size=" |
- | < | + | <span style=" |
- | <span style=" | + | <span style=" |
- | <span style=" | + | <span style=" |
- | <span style=" | + | <span style=" |
- | <span style=" | + | <span style=" |
- | <span style=" | + | <span style=" |
+ | <span style=" | ||
</ | </ | ||
- | Turn a string into an array by separating out the elements as bounded by a delimiter character | + | < |
====Parameters==== | ====Parameters==== | ||
- | < | ||
- | < | + | <html><b>< |
- | ====Return | + | < |
- | Array of elements | + | |
+ | ====Return | ||
+ | |||
+ | < | ||
====Example==== | ====Example==== | ||
- | <code php><? | + | |
- | $mylist=" | + | <code php> |
- | $myarray=explode($mylist,"," | + | <pre><? |
- | print_r($myarray); | + | $mylist=" |
- | /* result is: | + | $myarray=explode($mylist,"," |
- | p0 = ARRAY | + | print_r($myarray); |
- | STRING | + | ?></pre> |
- | STRING | + | </ |
- | STRING | + | |
- | STRING | + | The above example will output: |
- | */ | + | < |
- | ?></ | + | [$myarray] |
+ | (string) [0] => one | ||
+ | (string) [1] => two | ||
+ | (string) [2] => three | ||
+ | (string) [3] => four | ||
+ | ) | ||
+ | ?></pre> | ||
+ | </ | ||
+ | |||
+ | Note that if only one element is present (without the separator character) then a value of -1 will be returned. | ||
+ | |||
+ | <code php> | ||
+ | < | ||
+ | $mylist=" | ||
+ | $myarray=explode($mylist,"," | ||
+ | |||
+ | // the fix for a single parameter is to use is_array | ||
+ | if (!is_array($myarray)) { | ||
+ | $myarray=array($mylist); | ||
+ | } | ||
+ | print_r($myarray) | ||
+ | ?></ | ||
+ | </ | ||
====See Also==== | ====See Also==== | ||
- | [[implode()]] - Turn an array into a string | ||
- | [[print_r()]] - Dump the contents of an array to the current output | + | [[implode()]] - < |
- | [[sizeof()]] - Return the number of elements in an array | + | [[print_r()]] - Dump the contents of an < |
+ | |||
+ | [[sizeof()]] - Return the number of elements in an < | ||
[[uphp: | [[uphp: | ||
====Additional Information==== | ====Additional Information==== | ||
- | Note that there is an important difference in the parameter order between mainline PHP and Wattmon uPHP. In the mainline PHP function the delimiter comes first, but in uPHP the string to be exploded comes first: | + | |
+ | Note that there is an important difference in the parameter order between mainline PHP and Wattmon uPHP. In the mainline PHP function the delimiter comes first, but in uPHP the < | ||
< | < |