开发者

How do i convert this content to an array?

I have a plain text file which have the list of countries as follows.

United Kingdom
United States of America
Abkhazia
Afghanistan
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antarctica
Antigua and Barbuda
Argentina
Armenia
Aruba
Ashmore and Cartier Islands
Australia
Austria
Azerbaijan
Bahamas
Bahrain

I want to insert all the values in database for which i need to convert this to an array. i am using the following code for reading the file.

$fileContent = file_get_contents('countr开发者_开发百科ies.txt');
$fileContent = nl2br($fileContent);

Now i want to add ( , ) comma in the end of each line break. so that i can use explode() and convert it into an array. how do i do it?

thank you.


Not sure why you are doing nl2br

Just try

$fileContent = file_get_contents('countries.txt');
$array = explode("\n", $fileContent);


Use this function instead: file


Assuming the list you posted is actually in the following format after your nl2br() call

United Kingdom<br />
United States of America<br />
Abkhazia<br />
...

You can do

<?PHP
    explode("<br />", $yourString);
    // or explode("\n", $yourString); if you remove the nl2br() call
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜