Home > php教程 > PHP源码 > body text

php分割字符串并输出数组字符例子

WBOY
Release: 2016-06-08 17:21:57
Original
1643 people have browsed it

在php中分割字符函数可以使用explode()函数,但是使用此函数必须要有一个规律了,如以|分开或以其它字符分开,这样我们就可以直接使用explode把字符串分成数组之后再利用for遍历输出,下面来看几个例子。

<script>ec(2);</script>

explode() 函数把字符串分割为数组。

语法

explode(separator,string,limit)

例子一

 代码如下 复制代码

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

输出: 0.472347127 1.893372115 2.850965403

例子二

 代码如下 复制代码

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

输出: 893372115 472347127 850965403 PHP


逗号 分割字符串

利用 explode 函数分割字符串到数组

 代码如下 复制代码

$source = "hello1,hello2,hello3,hello4,hello5";//按逗号分离字符串
$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";
?>

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!