2014-01-27 27 views
-3

我想用PHP做些事情。如何使用PHP製作關鍵字列表的列表

我有名稱的列表:

first 
second 
third 
forth 

現在我想有這樣的:

first,second,third,forth 

怎麼能這樣呢一點點的PHP代碼?

+0

你在尋找'implode'嗎? – Barmar

+0

你的意思是?請在提問前提出您的問題 –

+0

,谷歌首先http://www.w3schools.com/php/showphp.asp?filename=demo_func_string_implode –

回答

2

假設你有這些數組

$arr=array('first','second'); 
$str=implode(",",$arr); 

EDIT(假設每個名字都上新的生產線,我將取代\ n,其中,)

$str=file_get_contents("filename.txt"); 
$newstr=str_replace("\n",',',$str); 
$newstr=trim($newstr, ",");//to remove the last comma 
echo $newstr; 
+0

謝謝,但如果我在TXT文件中有一個巨大的列表,我怎麼能把它們放入數組? – okey

+0

看到編輯好的答案..希望它能正常工作 – sumit

+0

真的很感謝,它的工作原理:X :) – okey

0

這真是基本的PHP,我不敢相信它在大多數教程中都沒有涉及。

$array = file("filename", FILE_IGNORE_NEW_LINES); 
$string = implode(',', $array); 
1

我想你基於新行的文字標籤,因此請試試

first 
second 
third 
forth 

    $arr = explode("\n", $txt); 
    echo implode(",",$arr); 

每一個新行都考慮爲一個數組值。

+0

謝謝,但如果我有一個巨大的TXT文件列表,我怎麼能把它們放入數組? – okey

+0

可以請你粘貼示例文本什麼內容在txt文件 –

+0

我說主TXT標籤包含像我在問題中寫的列表。 – okey

0

我認爲你正在尋找爆炸,像這樣http://us2.php.net/explode

array explode (string $delimiter , string $string [, int $limit ]) 

你要設置$delimiter' '$string可以複製 - >您的text.txt的內容粘貼。

做將是最好的事情:採取的text.txt內容和使用搜索替換刪除新線和添加空格(或逗號),讓你從

ron 
john 
kahn 

ron, john, kahn,在這點你可以導入一個數組在php中使用。