0

I have a file we will call info.txt under UNIX format that has only the following in it:

#Dogs
#Cats
#Birds
#Rabbits

and am running this against it:

$filename = "info.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd,filesize ($filename));

fclose ($fd);
$delimiter = "#";
$insideContent = explode($delimiter, $contents);

Now everything looks to be working fine except when I display the array I get the following.

[0] => 
[1] => Dogs
[2] => Cats
[3] => Birds
[4] => Rabbits

I checked the .txt file to make sure there wasn't any space or hidden characters in front of the first # so I'm at a loss of why this is happening other than I feel like I'm missing something terribly simple. Any ideas?

Thanks in advance!

4

5 に答える 5

6

explode()区切り文字で分割します。最初の区切り文字の前に何もない場合、それが最初の要素になります。何もない。空の文字列。

于 2010-03-17T00:06:49.657 に答える
2

これは、最初の文字が区切り文字であるためだと思います。したがって、空の文字列であっても、最初の要素の左側にあるものはすべて配置されます。したがって、ファイルは「#Dogs」ではなく「Dogs」で開始する必要があります。

于 2010-03-17T00:08:06.313 に答える
0

別の方法

$f=file("file");
print_r( preg_replace("/^#/","",$f) ) ;
于 2010-03-17T00:47:42.707 に答える
0

あなたはそれを次のように実行しています

#Dogs#Cats#Birds#Rabbits

PHPはそれをカットすることで分割します。したがって、犬がいる場合は「空白スペース」のように見えます。犬。

array_shift($ input、1);を使用すると、[0]を簡単に埋めることができます。

于 2010-03-17T00:08:29.623 に答える
0

改行によって爆発し、#をまったく使用しない可能性がありますが、その場合、末尾に空のアイテムがあります。解析後も、整合性チェック(空の場合は最初/最後の項目を削除)を行う必要があると思います。

于 2010-03-17T00:11:38.123 に答える