Jump to content
php.lv forumi

no txt failā no katras līnijas iegūt masīvu


Rich Bitch

Recommended Posts

Man ir txt fails ar šādu saturu:

1;es
2;tu
3;viņi
4;viņas

vajadzētu no šī txt faila iegūt šādus masīvus:

array("1", "es");
array("2", "tu");
array("3", "viņš");
array("4", "viņa");

 

saprotu, ka te laikam vajadzēs izmantot fread un explode, bet nekādīgi nevaru izdomāt un to realizēt

Edited by Rich Bitch
Link to comment
Share on other sites

labāk pirms explode() pielikt arī str_replace() un attīrīt faila rindiņu no newline simboliem

 

$a = file('data.txt');
echo '<textarea rows="25" cols="80">';
foreach($a as $s) print_r(explode(';', str_replace(array("\r", "\n"), '', $s)));
echo '</textarea>';

/*
Array
(
   [0] => 1
   [1] => es
)
Array
(
   [0] => 2
   [1] => tu
)
Array
(
   [0] => 3
   [1] => viņi
)
Array
(
   [0] => 4
   [1] => viņas
)
*/

Link to comment
Share on other sites

jā, jā, ka tik ne tā :D:D:D

 

http://lv.php.net/manual/en/function.file.php

Return Values

 

Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached.

es pat katram gadījumam to notestēju gan uz php4, gan php5, un visur šī funkcija strādāja tā, kā manuālis to reklamē...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...