PHP split string and output array character example

Source: Internet
Author: User
Tags explode split

The explode () function can be used to split character functions in PHP, but there must be a rule for using this function, such as separating the | or by other characters, so that we can use the explode to divide the string into an array and then use the for traversal output, here are a few examples.

The explode () function splits the string into arrays.

Grammar

Explode (Separator,string,limit)

Example One

The code is as follows

<?php
$test = ' 472347127,893372115,850965403 ';
$r =explode (",", $test);
for ($i =0; $i <sizeof ($r); $i + +)
{echo $i. "." $r [$i]. "";}
?>

Output: 0.472347127 1.893372115 2.850965403

Example Two

The code is as follows

<?php
$a = "893372115,472347127,850965403";
$b =explode (",", $a);
foreach ($b as $BB)
{echo $bb. "";//print_r ($b);}
?>

Output: 893372115 472347127 850965403 PHP

Comma split string

Dividing a string into an array using the EXPLODE function

The code is as follows

<?php

$source = "HELLO1,HELLO2,HELLO3,HELLO4,HELLO5";//separating strings by commas
$hello = Explode (', ', $source);

for ($index =0; $index <count ($hello); $index + +) {
echo $hello [$index];echo "</br>";
}

?>

Split function for character segmentation

  code is as follows &nbs P;

<?php

//separator can be a slash, dot, or horizontal line  
$date = "04/30/1973"; & nbsp
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.