Read specific word(number) from text file using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Johnymap
    New Member
    • Dec 2007
    • 10

    Read specific word(number) from text file using PHP

    Hi everyone

    I have text file which looks like these:

    "index.txt" Johan [Mabuya] 22 sebaya
    "home.hml" Mpho [KOKO] 23 leboa
    "index.frt" Tedesca [HIGH] 24 teba

    My problem is i want to read the number on the last sentence. At the current moment I can only read the whole of the last sentence. Please help
  • vituko
    New Member
    • Dec 2006
    • 48

    #2
    $last_num = preg_replace ('/^.* (\d+) [^ ]*$/s', '\\1', $string) ;

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Build a function to accept your LINES in the text file. and within the function do something similar to this;

      [CODE=php]<?php
      $string = '"index.txt" Johan [Mabuya] 22 sebaya';
      $str_array = explode(" ",$string);
      print_r($str_ar ray);
      print("<br>");
      // So the Number is in the 4th postion of the ARRAY
      print $str_array[3];
      ?>[/CODE]
      So the function should convert the string to a array. As I can see all your lines having a fixed format,right? So you can get the number by using its position as i did. hope this helps.

      Comment

      • Johnymap
        New Member
        • Dec 2007
        • 10

        #4
        Thank you very much. The code works perfectly.

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          No problem. post back to the forum any time.good luck.

          [EDIT: Errrm ! but is this "thank you note" for me??? :D ] Sorry if it is not for me.

          Comment

          • Johnymap
            New Member
            • Dec 2007
            • 10

            #6
            Hi everyone.

            The text file i am using has the following format:

            "2001_1.rtf " (1 January 2001) Ookame v Tombale (CCF No. 203 of 2001) [2001] BWHC 1
            "2001_2.rtf " (1 January 2001) Kgaimena v Leoifo (MISCA F.572 of 2001) [2001] BWHC 2
            "2002_1.rtf " (7 June 2002) Olebeng v The State (C A No. F104 of 2001) [2002] BWHC 1
            "2003_1.rtf " (1 January 2003) Sebogodi v Sebogodi (Matrimonial Cause No.F68 of 2003) [2003] BWHC 1

            So i have a dropdown form object populated with years, if the user select 2001(for eg.) on the form, I have to read the last value of that year from the text file which is "2001_2.rtf " (1 January 2001) Kgaimena v Leoifo (MISCA F.572 of 2001) [2001] BWHC 2
            do some processing and write back some new data to the text file under this specific line not at the end of file.

            Please help me

            Comment

            Working...