# PHP To Read a Text file

**URL:** <https://www.phphelp.com/t/php-to-read-a-text-file/35584>\
**Category:** Beginners - Learning PHP\
**Created:** [July 31, 2023, 3:39pm UTC](https://www.phphelp.com/t/php-to-read-a-text-file/35584 "2023-07-31T15:39:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PHPJames](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/e0b2c6/32.png) [@PHPJames](https://www.phphelp.com/u/PHPJames)\
**Post date:** [July 31, 2023, 3:39pm UTC](https://www.phphelp.com/t/php-to-read-a-text-file/35584/1 "2023-07-31T15:39:36Z")

</div>

Hi,

I am wanting to read a text file located on a NAS device \hg-nas01\Logs\Test\Test.txt

The file has times and dates of Windows Updates and i would like to get a few items from it this file is always being written to it be below is a example

```auto
----WINDOWS UPDATES----- @ 31/07/2023 11:03:58 AM
Beginning our update search. Please note, this may take a few minutes.
Windows update search has successfully completed. Beginning iteration of result set...
-----Windows Updates----- @ 31/07/2023 11:04:19 AM
Total Updates:- 2
Important:- 1
Optional :- 1
--------Total Time: 20.92 Sec----------------

Iteration of Windows Update Search Results has successfully completed.
-------------------------

```

i would like to get the Total Updates, Important and Optional but the will have several versions of this string.

Can anyone assist in giving some examples the way to tackle this the logfile is written to every hour.

---

<div class="post-metadata">

**Author:** ![katsry](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@katsry](https://www.phphelp.com/u/katsry)\
**Post date:** [July 31, 2023, 7:02pm UTC](https://www.phphelp.com/t/php-to-read-a-text-file/35584/2 "2023-07-31T19:02:07Z")

</div>

lets try this

```auto
<?php
// Get the file content
$file_content = file_get_contents("\\\\hg-nas01\\Logs\\Test\\Test.txt");

// Use regular expressions to find matches
preg_match_all("/Total Updates:- (\d+)/", $file_content, $total_updates);
preg_match_all("/Important:- (\d+)/", $file_content, $important_updates);
preg_match_all("/Optional :- (\d+)/", $file_content, $optional_updates);

// Print the results
foreach ($total_updates[1] as $index => $total) {
    echo "Total Updates: " . $total . "\n";
    echo "Important Updates: " . $important_updates[1][$index] . "\n";
    echo "Optional Updates: " . $optional_updates[1][$index] . "\n\n";
}
?>

```

Please replace the `\\\\hg-nas01\\Logs\\Test\\Test.txt` path with your actual NAS path if different.

Also, ensure that the PHP script has the necessary read permissions to access the file on the NAS. Sometimes, you may need to mount the NAS as a network drive in your operating system and then use the local mount path.

Hope this works for you, good luck

---

<div class="post-metadata">

**Author:** ![PHPJames](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/e0b2c6/32.png) [@PHPJames](https://www.phphelp.com/u/PHPJames)\
**Post date:** [August 1, 2023, 8:10am UTC](https://www.phphelp.com/t/php-to-read-a-text-file/35584/3 "2023-08-01T08:10:03Z")

</div>

Hi Katsry,

I am getting a error of the following on testing your code i will try and work this out well hopefully 😄

```auto
PHP Warning: Undefined array key 0 in C:\inetpub\wwwroot\SupportDesk\Test.php on line 13
PHP Warning: Undefined array key 0 in C:\inetpub\wwwroot\SupportDesk\Test.php on line 14
PHP Warning: Undefined array key 1 in C:\inetpub\wwwroot\SupportDesk\Test.php on line 13
PHP Warning: Undefined array key 1 in C:\inetpub\wwwroot\SupportDesk\Test.php on line 14
PHP Warning: Undefined array key 2 in C:\inetpub\wwwroot\SupportDesk\Test.php on line 13
PHP Warning: Undefined array key 2 in C:\inetpub\wwwroot\SupportDesk\Test.php on line 14

```

---

<div class="post-metadata">

**Author:** ![katsry](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@katsry](https://www.phphelp.com/u/katsry)\
**Post date:** [August 3, 2023, 8:01am UTC](https://www.phphelp.com/t/php-to-read-a-text-file/35584/4 "2023-08-03T08:01:28Z")

</div>

The warning is indicating that the array keys you’re trying to access do not exist. This can happen when the regular expressions do not find any matches in the file content, so the resulting arrays `$total_updates[1]` , `$important_updates[1]` , and `$optional_updates[1]` are empty.

```auto
// Check if matches were found before printing the results
if(!empty($total_updates[1]) && !empty($important_updates[1]) && !empty($optional_updates[1])){
    foreach ($total_updates[1] as $index => $total) {
        echo "Total Updates: " . $total . "\n";
        echo "Important Updates: " . $important_updates[1][$index] . "\n";
        echo "Optional Updates: " . $optional_updates[1][$index] . "\n\n";
    }
} else {
    echo "No updates found.";
}

```

Keep in mind that this solution assumes that for every “Total Updates”, there is an “Important Updates” and an “Optional Updates” value, and they all have the same index. If that’s not the case, you would need a more robust solution to handle missing values.
