2017-10-11 82 views
-3

我正在尋找一種方法將字符串變量分成兩部分。舉個例子,如果 我:如何分割一個PHP變量

$activities = "In the morning we will dance, in the evening we will sing" 

我想這個分成,

$activities1 = "In the morning we will dance" 
$activities2 = "In the evening we will sing" 

我希望得到任何幫助。

感謝

+1

使用爆炸功能 $陣列=爆炸( '',$活動); print_r($ array); –

+2

[PHP子串提取的可能重複。獲取第一個'/'或整個字符串之前的字符串](https://stackoverflow.com/questions/1935918/php-substring-extraction-get-the-string-before-the-first-or-the-whole -strin) – madshvero

+2

我很困惑..如果你在谷歌上輸入「如何分割一個PHP變量」(這個問題的標題),第一個條目是來自php.net的explode()函數......真的有人在他們甚至嘗試谷歌之前問stackoverflow? – Twinfriends

回答

5

使用爆炸爲分裂變量
http://php.net/manual/en/function.explode.php

// Example1 
$pizza = "porcion1 porcion2 porcion3 porcion4 porcion5 porcion6"; 
$porciones = explode(" ", $pizza); 
echo $porciones[0]; // porción1 
echo $porciones[1]; // porción2 

您可以設置分割字符這裏explode("HERE", $var);

+0

要完成答案,請使用'ucfirst()'函數將字符串的第一個字母設置爲大寫 –

1

您需要使用爆炸功能。可以說這是你的變量..

$activities = "In the morning we will dance, in the evening we will sing" 

你將通過使用PHP explode()函數分割。

$activities = explode(",", $activities); 

這將,$activities拆分句子,並使用ucfirst做出的第一個字母爲大寫。

echo $activities[0]; 
echo ucfirst($activities[1]); 

參考: