Php splits strings and outputs array character examples

Source: Internet
Author: User

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 ";
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.