Picking up specific text from a text file using PHP

Asked

Viewed 617 times

-1

I was wondering if you have how to get a specific text from a txt file using php ?

I can already read this to display it completely as a result in a div, but I needed to take for example a word or a number to display separately in another location. You can do this in PHP or using Javascript ?

Below is my div where displays the results that run the PHP script that reads the text file:

<div id="box_results">
    <div id="results" class="results">
      <?php
        //Abrimos o arquivo em leitura
        $arquivo = 'log/speedtest.txt';
        $fp = fopen($arquivo, 'r');

        //Lemos o arquivo
        $texto = fread($fp, filesize($arquivo));

        //Transformamos as quebras de linha em etiquetas <br>
        $texto = nl2br($texto);

        echo $texto;
      ?>
    </div>  
  </div>

I ask this because this text file is temporary, it is generated every time a form is submitted, ie it updates the information in this text file. And for the results to be displayed after submitting a form, I use the following script:

<script type="text/javascript">
$("#frm").on("submit", function(){
    $("#results").html('');
    fun();
});

var t; 

function recarrega() {
    $("#box_results").load(" #results");
    $("#execute").hide();
} 

function fun() {
    t = setInterval(recarrega, 1000);
} 

function stop() {
    clearTimeout(t);
    $("#execute").show();
}
</script>

You can then pick up specific content after this text file is updated ?

Edit:

This is the contents of the text file I will read:

[Parâmetros]
CPU = 512
WAN = 1237
LAN1 = 435
  • As strange as this file saving solution seems to be, but it would no longer be convenient to use a structured text pattern like JSON, XML, CSV, etc?

  • @Andersoncarloswoss So, in the case I don’t have much knowledge, and I ended up investing in this method that I still had help to do. And in case they are not sensitive information to be saved in the text file then there is no problem in this case they are displayed.

  • @But still in this case, I need to find out if you have the ability to do this because I need to read another type of information, which in this case is a speed test, I needed to get information like the CPU of a computer or WAN that will all be recorded in a text file.

1 answer

4

You can use the file_get_contents along with the function strpos.

It would be something like this:

if (strpos(file_get_contents($arquivo), 'palavra_a_ser_procurada') !== false)  {
  echo 'found';
}
  • The file_get_contents transforms the file text into a string, can be worked as such in PHP.

  • The strpos serves for you to find the position of the first occurrence of a word within a string. You can also check the position that the string was found inside the text, as per you can see in the manual in the link below.

  • To pick up part of the text you can use functions like the substr().

As you said you will take data from the machine, such as CPU, for example, you can create a function that identifies if you find the CPU word inside the file and, if you find it, it informs the position and based on it you can get x letters in front of the substr.

Functions manual in the PHP page so you can better understand and assemble your code:

https://www.php.net/manual/en/function.file-get-contents.php

https://www.php.net/manual/en/function.strpos.php

https://www.php.net/manual/en/function.substr.php

  • In case there’s any way I can get a number that’s random ? As I commented above with another boy, it will be for a system where you will have a file with a number followed by a text. Ex.: CPU = 512. But this number will change as this system is updated, and I needed to pass this number to a URL.

  • I edited the answer by adding information. But basically, yes, there is. Even with the value being random, the data you’re going to get there wouldn’t be. Then you can search for CPU, see the position and then get the next 3 digits counting 6 positions forward, for example

  • Interesting, can you set up the scheme for me, as I am apprentice in PHP still, I’m a little lost? Having the information contained in the text file to be able to take what I need and then pass to PHP ?

  • 1

    It is difficult to assemble more things than this considering that I do not have the file, but the logic is there: make an if to check if the word exists. If it exists it takes the position. After that you account qts positions will have up to the value you want and then you display it. The hint is how many digits you will want to display. In "CPU=512" and C at position 1, vc will display 3 characters from position 5. If you look at the examples in the manual helps to do as well, understand more each function...

  • I edited the question with the parameters I need to read, now this code you gave me, I put it inside the mounted function to read the text file ? Where can I put ?

  • Another question is, after I can find the text and get the numbers, I can pass them to a URL ? as if they were a page ID you know ?

  • 1

    As I put it there and explain it better in the manual, file_get_contents turns the contents of the file into a string, so you can work with it as such normally, like other qqr text in your php. You can store it in a variable and do whatever you want with it...

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.