Rich Bitch Posted November 28, 2009 Report Share Posted November 28, 2009 (edited) 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 November 28, 2009 by Rich Bitch Quote Link to comment Share on other sites More sharing options...
marcis Posted November 28, 2009 Report Share Posted November 28, 2009 $lines = file('fails.txt'); foreach($lines as $line){ print_r(explode(',', $line)); } Quote Link to comment Share on other sites More sharing options...
2easy Posted November 28, 2009 Report Share Posted November 28, 2009 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 ) */ Quote Link to comment Share on other sites More sharing options...
marcis Posted November 28, 2009 Report Share Posted November 28, 2009 2easy -> file() newline'us uztver kā jaunu rindiņu, kas attiecīgi ir jauns masīva elements. Tas nozīmē, ka masīvā $lines nebūs elementu, kas satur newline'us. Quote Link to comment Share on other sites More sharing options...
2easy Posted November 28, 2009 Report Share Posted November 28, 2009 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ē... Quote Link to comment Share on other sites More sharing options...
marcis Posted November 28, 2009 Report Share Posted November 28, 2009 My bad, točna. Tas, ka viņa explodē pa newline, tak nenozīmē, ka newline būs aizvākti. Jebkurā gadījumā es labprātāk izvēlētos lietot trim(), lieko figņu aizvākšanai no stringa priekšanas un pakaļas. Quote Link to comment Share on other sites More sharing options...
2easy Posted November 28, 2009 Report Share Posted November 28, 2009 oo jā, šajā gadījumā trim ir ērts str_replace(array("\r", "\n"), '', $s) varētu aizvietot ar rtrim($s, "\r\n") tā kā space/tab var arī nebūt tik "lieki" (atkarībā no situācijas), tad labāk trimot tikai newline charus Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.