I want to do something with PHP .
I have this list of names :
first
second
third
forth
Now i want to have this :
first,second,third,forth
How can this happen with a little PHP code ?
Assuming that you have those in array
$arr=array('first','second');
$str=implode(",",$arr);
EDIT(assuming each names are on the new line, i will replace with ,)
$str=file_get_contents("filename.txt");
$newstr=str_replace("
",',',$str);
$newstr=trim($newstr, ",");//to remove the last comma
echo $newstr;
This is really basic PHP, I can't believe it isn't covered in most tutorials.
$array = file("filename", FILE_IGNORE_NEW_LINES);
$string = implode(',', $array);
i think you text tag based on new line, so try this
first
second
third
forth
$arr = explode("
", $txt);
echo implode(",",$arr);
every new line consider as a array value.
I think you're looking for explode, like so http://us2.php.net/explode
array explode ( string $delimiter , string $string [, int $limit ] )
You want to set $delimiter
to ' '
, $string
can be a copy -> paste of your text.txt content.
The best thing to do would be: take the text.txt content and use search-replace to remove new lines and add spaces (or commas), so you go from
ron
john
kahn
to ron, john, kahn
, at which point you can import an array for use in php.