Php splits strings and outputs array characters. example _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php splits the string and outputs an array character example. Php splits the string and outputs an array character example. in php, you can use the explode () function to split the character. However, this function must have a regular pattern, for example, the string is separated by | or separated by other words, and the array character is output.

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:

$ Test = '2014 372115,850965403 ';
$ R = explode (",", $ test );
For ($ I = 0; $ I {Echo $ I. ".". $ r [$ I]. "";}
?>

Output: 0.472347127 1.893372115 2.850965403

Example 2

The code is as follows:

$ 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:

$ Source = "hello1, hello2, hello3, hello4, hello5"; // separate strings by commas
$ Hello = explode (',', $ source );

For ($ index = 0; $ index Echo $ hello [$ index]; echo"
";
}

?>

Split function for Character Segmentation

The code is as follows:

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

The delimiter function can use the explode () function to separate characters in php, but this function must have a regular pattern, for example, separating with | or using other words...

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.