Home  >  Article  >  Backend Development  >  PHP splits a string and outputs an array of characters example_PHP tutorial

PHP splits a string and outputs an array of characters example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:34911browse

Example of php splitting a string and outputting an array of characters

The explode() function can be used to split character functions in php, but there must be a rule for using this function, such as | separated by other characters, so that we can directly use explode to divide the string into an array and then use for to traverse the output. Let's look at a few examples.

 The explode() function splits a string into an array.

Grammar

explode(separator,string,limit)

Example 1

The code is as follows

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

 代码如下  

$test='472347127,893372115,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,472347127,850965403" ;
$b=explode(",",$a);
foreach($b as $bb)
{ echo $bb.""; //print_r($b); }
?>

 代码如下  

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

Output: 893372115 472347127 850965403 PHP

Comma splits string

Use explode function to split string into array

The code is as follows

代码如下

$source = "hello1,hello2,hello3,hello4,hello5";//按逗号分离字符串
$hello = explode(',',$source);

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

?>

$source = "hello1,hello2, hello3,hello4,hello5";//Separate strings by commas
$hello = explode(',',$source);

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

?>

  split函数进行字符分割

 代码如下  

代码如下

// 分隔符可以是斜线,点,或横线
$date = "04/30/1973";
list($month, $day, $year) = split ('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year
n"; 
?>

// 分隔符可以是斜线,点,或横线 
$date = "04/30/1973"; 
list($month, $day, $year) = split ('[/.-]', $date); 
echo "Month: $month; Day: $day; Year: $year
n"; 
?>

http://www.bkjia.com/PHPjc/873237.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/873237.htmlTechArticlephp分割字符串并输出数组字符例子 在php中分割字符函数可以使用explode()函数,但是使用此函数必须要有一个规律了,如以|分开或以其它字...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn