Jump to content

Read from a text file after a specific word


raytaylornz

Recommended Posts

Hi all.

 

I have a text file that looks like the one below.

I know how to open the text file but I need to get the php pointer to take a value suck as $username. Once it finds $username, i need it to move to the next following MonthUp"> and then read the number after it and save it to the string $monthup. I will be doing the same for $monthdown and then closing the file.

 

Eg. If the value is $username, i need to find the first occurance of that username, find the number after MonthUp"> and read it to $monthup, then move to MonthDown"> and then read the number after it to $monthdown.

I will then use php to add the numbers together so that I can show the user when they enter their username into the website, how much they have used for the month.

 

I am just learning php and all I can find so far from google is reading line-by-line which i want to read after a specific word as I need to do this for a few different files and line by line wont work. 

 

 

File - stats.cfg - can be renamed to stats.txt easily.

<config>

<list name="UserStats">

  <listitem>

    <variable name="Id">288A5772-2964-4701-9E66-765F3CECE522</variable>

    <variable name="Name">all users</variable>

    <variable name="DayUp">32729762</variable>

    <variable name="DayDown">336451305</variable>

    <variable name="WeekUp">982718994</variable>

    <variable name="WeekDown">5711384480</variable>

    <variable name="MonthUp">15542938381</variable>

    <variable name="MonthDown">106022753555</variable>

    <variable name="TotalUp">441879285577</variable>

    <variable name="TotalDown">1763579705023</variable>

    <variable name="Since">1210849661</variable>

    <variable name="Timestamp">1256258234</variable>

    <variable name="FullName">all users</variable>

  </listitem>

  <listitem>

    <variable name="Id">807459fd-71a9-467d-bf0c-f0ce98fef937</variable>

    <variable name="Name">Administrator</variable>

    <variable name="DayUp">0</variable>

    <variable name="DayDown">0</variable>

    <variable name="WeekUp">0</variable>

    <variable name="WeekDown">0</variable>

    <variable name="MonthUp">0</variable>

    <variable name="MonthDown">0</variable>

    <variable name="TotalUp">995621942</variable>

    <variable name="TotalDown">6089875753</variable>

    <variable name="Since">1210849662</variable>

    <variable name="Timestamp">1256224866</variable>

    <variable name="FullName"></variable>

  </listitem>

  <listitem>

    <variable name="Id">9cd97dd4-f902-4811-ad05-372658c8ec64</variable>

    <variable name="Name">Mathew</variable>

    <variable name="DayUp">0</variable>

    <variable name="DayDown">0</variable>

    <variable name="WeekUp">0</variable>

    <variable name="WeekDown">0</variable>

    <variable name="MonthUp">36809697</variable>

    <variable name="MonthDown">565151504</variable>

    <variable name="TotalUp">1739819196</variable>

    <variable name="TotalDown">26628701150</variable>

    <variable name="Since">1210849662</variable>

    <variable name="Timestamp">1256257815</variable>

    <variable name="FullName">mathew</variable>

  </listitem>

Link to comment
Share on other sites

Use simplexml, it is made for this!

 

Does it work with non-xml files?

I dont think this file is proper xml, and the other files that I need to pull information from are csv and plain text files. I would need to pull information from these other files in the same way as i am wanting to do this.

Link to comment
Share on other sites

Other than no closing for the config and list tags, this looks fine to me, but I guess you'd have to check the manual to know for sure.  CSV is perfect for arrays, which lend themselves to other types of operations.  Plain old strings are good for strpos, substr, and regular expressions.

 

 

Link to comment
Share on other sites

You probably have it, try this:

 

$xml = new SimpleXMLElement();

 

Does it throw an exception?  If so, you're good.

 

Just phoned the web host - they say it has something to do with php5 and i am only able to use php 4 or something like that.

 

Is there no way to use php's built in file reading methods to just search for a specific word and then capture the text between X and Y?

Link to comment
Share on other sites

Another option is I could delete everything from the start of the text file up to the username eg. Mathew

 

Then I could delete everything before the monthup'>

Then I could delete everything after the first </ and be left with a text file of just the value I want. then take the whole text file and save it as the string value $monthup

 

Does anyone know how to do this? All I can find is how to delete before and after a length in bytes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.