Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
dvprog_09 [2013-01-28 13:46] Daniel Viström |
dvprog_09 [2019-12-06 16:09] Daniel Viström |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Genomgång | + | ====== Genomgång - Att läsa från och skriva till textfiler ====== |
+ | <php> | ||
+ | /* | ||
Tutorial:\\ | Tutorial:\\ | ||
[[http:// | [[http:// | ||
[[http:// | [[http:// | ||
+ | */ | ||
+ | </ | ||
<code php> | <code php> | ||
Line 16: | Line 20: | ||
| | ||
*/ | */ | ||
- | include(' | + | include ' |
// Skriver till en textfil. Finns inte filen skapas den. FILE_APPEND gör att innehåll läggs till och inte skrivs över. | // Skriver till en textfil. Finns inte filen skapas den. FILE_APPEND gör att innehåll läggs till och inte skrivs över. | ||
file_put_contents(' | file_put_contents(' | ||
- | // Läser en textfil till en sträng. | + | $textfil = ' |
- | $str = file_get_contents(' | + | // Läser en textfil till en sträng. |
- | echo nl2br($str) . '< | + | $str = file_get_contents($textfil); |
+ | echo nl2br($str) . '< | ||
// Läser in en textfil till en array, radbrytning ger ny " | // Läser in en textfil till en array, radbrytning ger ny " | ||
+ | // Varje rad i filen har följande format: | ||
$arr = file(' | $arr = file(' | ||
// print_r($arr); | // print_r($arr); | ||
Line 32: | Line 38: | ||
while ($i < count($arr)) { | while ($i < count($arr)) { | ||
$row = explode(':', | $row = explode(':', | ||
- | // print_r($row); | + | // print_r($row); |
- | // Skriver bara ut de som heter Johansson | + | // Skriver bara ut de som heter Svensson |
- | if (trim($row[2]) == 'Johansson') { // trim tar bort radbrytning. | + | if (trim($row[2]) == 'Svensson') { // trim tar bort radbrytning. |
$temp = ' | $temp = ' | ||
- | file_put_contents(' | + | file_put_contents(' |
} | } | ||
$i++; | $i++; | ||
} | } | ||
- | include(' | + | include ' |
</ | </ | ||