php array, only last item in array is correct -
below code should create problem clear. code irrelevant problem has been left out, e.g. fetching contents of url, printing contents new file.
can explain why variables jumbled except lastly item?
<?php $f = fopen("cucina2.txt", "r"); // read line line until end of file while (!feof($f)) { // create array using newline delimiter $arrm = explode("\n",fgets($f)); //add word url address $url = 'hhttp://www.some.com/vocab/' . $arrm[0] . '/'; //create text file word $glossary = $arrm[0]; $glossary .= '.txt'; //check functions echo "arrm is: "; echo $arrm[0]; echo "\n"; echo "glossary is: "; echo $glossary; echo "\n"; echo "url is: "; echo $url; echo "\n"; } ?>
cucina.txt:
batticarne battuto bavarese bavetta
results:
arrm is: batticarne .txtsary is: batticarne /rl is: hhttp://www.some.com/vocab/batticarne arrm is: battuto .txtsary is: battuto /rl is: hhttp://www.some.com/vocab/battuto arrm is: bavarese .txtsary is: bavarese /rl is: hhttp://www.some.com/vocab/bavarese arrm is: bavetta glossary is: bavetta.txt url is: hhttp://www.some.com/vocab/bavetta/
it appears info file (cucina.txt) contains windows-style line breaks ("\r\n
"). when split input lines @ \n
, left trailing carriage homecoming \r
@ end of each slice.
try instead:
$arrm = preg_split('/[\r\n]+/',fgets($f));
also, aware while (!feof($f))
wrong. should check eof when reading file, not @ top of loop. way avoid processing empty lines.
while (!feof($f)) { $s = fgets($f); if (!$s) break; $arrm = preg_split('/[\r\n]+/',$s); : : }
php arrays
No comments:
Post a Comment