Heim  >  Artikel  >  Backend-Entwicklung  >  php分割字符串并输出数组字符例子_PHP教程

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

WBOY
WBOYOriginal
2016-07-13 10:19:34887Durchsuche

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

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

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/873237.htmlTechArticlephp分割字符串并输出数组字符例子 在php中分割字符函数可以使用explode()函数,但是使用此函数必须要有一个规律了,如以|分开或以其它字...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn