Getting certain values from a text file with PHP?

Associate
Joined
29 Dec 2004
Posts
2,253
Let's say i have a text file containing values like this:

* 01/01/2001 - 05/01/2001 Test Text
* 31/12/2004 - 01/01/2005 More Tests
* 04/05/2008 - 02/06/2008 Hello again
* 01/10/2008 - 03/10/2008 Testypie

..could i use PHP to display a page that would display anything in the text file where the dates were, say, a month ahead of the first date listed (when i say first, i mean the first of the two..e.g. 04/05/2008 on the third line)? For example, if i were to view the PHP page now, it would output "01/10/2008 - 03/10/2008 Testypie".

I have Googled around a bit, and can find everything i could ever want to do with a text file...bar sorting it! Any help much appreciated.
 
Yes you can.

I don't use PHP too much but I would read the file line by line (assuming it is formatted like you suggest) and parse the string into a couple of date objects and a string. You can then perform tests on the dates and if they meet your requirements, print out the string.
 
Do you have any example code i could use? I know how to open and write to files like i mentioned but am not sure how to search through for certain values etc...
 
Afraid not.

I think you are going about it the wrong way, or at least thinking about going about it the wrong way. When you say you want to search for values this effectively suggests that you will loop through the file and check each date. So try looking for examples of parsing text files using PHP, they will help you to get started.

Is the text fragment you posted exactly the same as the file you will be working on? Also, do you have any control over the format of this file, or is it created by some other software?
 
Thanks guys, i'll have a look around. The text fragment is exactly the same as the format i'm using, which unfortunately cannot be changed as it is used for a PHP app that displays a Calendar in Dokuwiki.
 
Not a problem as it is still doable but it could have been a csv file, for instance, which would make parsing easier as you can just split the string by commas and look at the constituent parts. In this example I imagine working with substrings would be easier.
 
Another quick one:

If i have a text file with this content:

AA - Mr Test0, The House0, 10 House Road, Testerton, London, AA1 1AA.
AA - Mr Test1, The House1, 11 House Road, Testville, London, AA1 1AA.
AB - Mr Test2, The House2, 12 House Road, Testtown, London, AA1 1AA.
AB - Mr Test3, The House2, 13 House Road, Testingtontown, London, AA1 1AA.

And i currently get "Mr Test0", "Mr Test1"...etc with:

Code:
$TheSite = $_POST['address'];
	$TheSite = substr($TheSite,strpos($TheSite,'-')+1);
		$TheSite = substr($TheSite,0,strpos($TheSite,','));
	$TheSite = trim($TheSite);
$StartDate = $_POST["startdate"];

How can i adapt that so i can output
"Mr Test0, The House0"
"Mr Test1, The House1"

etc, rather than

"Mr Test0"
"Mr Test1"

...?
 
Last edited:
You would be better of loading the individual parts into an array and then outputting the individual elements:

echo $array[1] . ", " . $array[2];

something like that - but like i said, not a PHP guy my self.
 
Ah it's alright, it's all working as is (and i'm not sure how to re-write it) so i'm just looking for how to tweak the substr bit to make it work by going to the next comma...
 
How about:

PHP:
$TheSite = $_POST['address'];
$stripped_dash = explode(" - ", $TheSite);
$part_array = explode(", ", $stripped_dash[1]);
echo $part_array[0] . ", " . $part_array[1];

Would output:
"Mr Test0, The House0"
"Mr Test1, The House1"

etc.
 
Sorry guys i don't need an array, i'm only getting the $_POST['address'] value which is in the format of "AA - Mr Test0, The House0, TestTown..." etc and i just want to format it so it only contains the value "Mr Test0, The House0".

I think i just need to amend the substr bit to go between the hyphen "-" and the second comma (after "The House0") rather than between the hyphen and the first comma (after "Mr Test0".)

Thanks
 
Sorry i think it's this bit i need to change:

Code:
strpos($TheSite,',')

I need it to get the second comma, not the first one from a sentence like this:

AA - A test, Test house, Test Town, West Testy, UK

Can anyone help? Is this do-able?
 
PHP:
$array = explode(', ', 'AA - A test, Test house, Test Town, West Testy, UK');
echo $array[0]; //AA - A test
echo $array[1]; //Test house
echo $array[2]; //Test Town
//etc

all this is very horrible though. don't you have any control over this data you're using?
 
Afraid not, i'm basically just integrating my very very simple script with something much bigger. Thanks for the above! I guess it's not possible with strpos...
 
Back
Top Bottom