By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,203 Members | 1,401 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,203 IT Pros & Developers. It's quick & easy.

Read specific word(number) from text file using PHP

P: 10
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
Dec 18 '07 #1
Share this Question
Share on Google+
5 Replies


P: 48
$last_num = preg_replace ('/^.* (\d+) [^ ]*$/s', '\\1', $string) ;
Dec 18 '07 #2

ak1dnar
Expert 100+
P: 1,584
Build a function to accept your LINES in the text file. and within the function do something similar to this;

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $string = '"index.txt" Johan [Mabuya] 22 sebaya';
  3. $str_array = explode(" ",$string);
  4. print_r($str_array);
  5. print("<br>");
  6. // So the Number is in the 4th postion of the ARRAY
  7. print $str_array[3];
  8. ?>
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.
Dec 18 '07 #3

P: 10
Thank you very much. The code works perfectly.
Dec 19 '07 #4

ak1dnar
Expert 100+
P: 1,584
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.
Dec 20 '07 #5

P: 10
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
Jan 11 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.