{"id":12874,"date":"2022-11-09T09:41:01","date_gmt":"2022-11-09T07:41:01","guid":{"rendered":"https:\/\/www.beseit.net\/?p=12874"},"modified":"2022-11-12T18:43:16","modified_gmt":"2022-11-12T16:43:16","slug":"leer-un-archivo-linea-a-linea-con-php","status":"publish","type":"post","link":"http:\/\/www.beseit.net\/?p=12874","title":{"rendered":"Llegir un fitxer l\u00ednia per l\u00ednia amb PHP"},"content":{"rendered":"<p>Hi ha dos m\u00e8todes de fer-ho<\/p>\n<h2 id=\"toc-aw8y-reading-the-whole-file-line-by-line\">Utilitzant file() per llegir el fitxer<\/h2>\n<p>[php]<\/p>\n<p>&lt;?php<\/p>\n<p>$aux = file(&#8216;wordlist_ger.txt&#8217;,FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES);<\/p>\n<p>$count = 0;<br \/>\nforeach($aux as $linia) {<br \/>\n    $count += 1;<br \/>\n    echo $linia.&quot;&lt;br&gt;&quot;;<br \/>\n}<\/p>\n<p>echo &quot;&lt;br&gt; UN ALTRA FORMA DE FER-HO &lt;br&gt;.&quot;;<\/p>\n<p>$num_linies=count($aux); \/\/conta el numero d&#8217;elements de l&#8217;array $aux<\/p>\n<p>for ($i=0; $i &lt;$num_linies ; $i++) {<br \/>\n\techo &quot;&lt;br&gt; $aux[$i]&quot;;<br \/>\n}<\/p>\n<p>?&gt;<\/p>\n<p>[\/php]<\/p>\n<p><a href=\"https:\/\/www.beseit.net\/wp-content\/uploads\/2022\/11\/php_file.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.beseit.net\/wp-content\/uploads\/2022\/11\/php_file.png\" alt=\"\" width=\"1002\" height=\"546\" class=\"aligncenter size-full wp-image-12937\" srcset=\"http:\/\/www.beseit.net\/wp-content\/uploads\/2022\/11\/php_file.png 1002w, http:\/\/www.beseit.net\/wp-content\/uploads\/2022\/11\/php_file-300x163.png 300w, http:\/\/www.beseit.net\/wp-content\/uploads\/2022\/11\/php_file-768x418.png 768w, http:\/\/www.beseit.net\/wp-content\/uploads\/2022\/11\/php_file-500x272.png 500w\" sizes=\"auto, (max-width: 1002px) 100vw, 1002px\" \/><\/a><\/p>\n<p>===============================================================<br \/>\n<strong>Utilitzant fgets() per llegir el fitxer<\/strong><\/p>\n<p>[php]<br \/>\n&lt;?php<br \/>\n\/\/require(&#8216;..\/func.php&#8217;);\/\/ func.php es la llibreria de funcions<\/p>\n<p>\/\/El primer que haurem de fer \u00e9s obrir el fitxer .txt en mode lectura:<\/p>\n<p>$archivo = fopen(&#8216;pons_ger.txt&#8217;,&#8217;r&#8217;);<\/p>\n<p>\/\/Tot seguit podem rec\u00f3rrer les l\u00ednies de l\u2019arxiu mitjan\u00e7ant un bucle:<\/p>\n<p> while ($linea = fgets($archivo)) {<\/p>\n<p> echo $linea.&quot;&lt;br&gt;;&quot;;<\/p>\n<p>$aux[] = $linea;<\/p>\n<p>$numlinea++;<\/p>\n<p>}<\/p>\n<p>\/*<br \/>\nA cada iteraci\u00f3 d\u2019aquest bucle estem llegint una nova l\u00ednia del fitxer, l\u2019imprimim<br \/>\n per pantalla i el guardem en un vector per processar cada l\u00ednia despr\u00e9s.<br \/>\n La variable $numlinea la podem utilitzar per realitzar una acci\u00f3 quan estiguem<br \/>\nllegint segons quina l\u00ednia.<br \/>\n*\/<\/p>\n<p>fclose($archivo);<\/p>\n<p>\/\/I finalment, recorrem l\u2019array amb les l\u00ednies de l\u2019arxiu:<\/p>\n<p>echo &#8216;&lt;pre&gt;&#8217;;<\/p>\n<p>print_r($aux);<\/p>\n<p>echo &#8216;&lt;\/pre&gt;&#8217;;<\/p>\n<p>?&gt;<br \/>\n[\/php]<\/p>\n<p><a href=\"https:\/\/www.beseit.net\/wp-content\/uploads\/2022\/11\/Imatge1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-12884\" src=\"https:\/\/www.beseit.net\/wp-content\/uploads\/2022\/11\/Imatge1.png\" alt=\"\" width=\"1326\" height=\"883\" srcset=\"http:\/\/www.beseit.net\/wp-content\/uploads\/2022\/11\/Imatge1.png 1326w, http:\/\/www.beseit.net\/wp-content\/uploads\/2022\/11\/Imatge1-300x200.png 300w, http:\/\/www.beseit.net\/wp-content\/uploads\/2022\/11\/Imatge1-1024x682.png 1024w, http:\/\/www.beseit.net\/wp-content\/uploads\/2022\/11\/Imatge1-768x511.png 768w, http:\/\/www.beseit.net\/wp-content\/uploads\/2022\/11\/Imatge1-451x300.png 451w\" sizes=\"auto, (max-width: 1326px) 100vw, 1326px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi ha dos m\u00e8todes de fer-ho Utilitzant file() per llegir el fitxer [php] &lt;?php $aux = file(&#8216;wordlist_ger.txt&#8217;,FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES); $count = 0; foreach($aux as $linia) { $count += 1; echo $linia.&quot;&lt;br&gt;&quot;; } echo &quot;&lt;br&gt; UN ALTRA FORMA DE FER-HO &lt;br&gt;.&quot;; $num_linies=count($aux); \/\/conta &hellip; <a href=\"http:\/\/www.beseit.net\/?p=12874\">Continua llegint <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[],"class_list":["post-12874","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts\/12874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12874"}],"version-history":[{"count":22,"href":"http:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts\/12874\/revisions"}],"predecessor-version":[{"id":12938,"href":"http:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts\/12874\/revisions\/12938"}],"wp:attachment":[{"href":"http:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12874"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}