expiration date?

well i’ve got a question: I’m actively searching for a code which removes a specific line from a file in filezilla after some time

File: Test.txt

Hello you! < expiration date 5/11/2015 09:00AM
wassup? < expiration date 6/11/2015 09:05AM

this is a short example the real file have loads of lines.
if theres a code for this it would be awesome if not, id be glad if you guys can redirect me to a tutorial for it
thanks alot for the help guys.

I suggest you should create a parsing loop to look at each line in the file…

You would need to read all of the lines in your file and check each one for the expiration date. It really depends on how you acquire the file. If it is created from a PHP program, you can format the date so that
it is easy to pull out of each line. If not, you have to parse thru all of the lines in the file and locate the ones
that are expired. Loosely, grab each line from the file. Place each line into a variable. Then, test it for the
correct expression. If each of your lines are always in the exact format you posted, you can grab the data
with ease. Let’s say the line is in a variable named $line…

$start = strpos($line, “<”);
$date = trim(substr($line, $start));

This would give you the string after the “<” which should be your date. ( In text form, that is! )
Then, you would need to compare it to the current date by using it in a date format instead of text.
You can get today’s date/time like this:

$today = date(“Y-m-d H:i:s”); You will have to check the PHP “date()” function to match it to your layout.

Then, you can compare with something like: if ($date < $today) { do not pass this line to output… }

Well, there you have a place to start with… Either way, you will have to create a loop to check each line,
one at a time to check the dates… Good luck, show us some code if you have issues…

First, I love this theory! Some code actually has to be created, there are not tutorials for everything that anyone wants.

Second, what portion are you trying to remove? The entire line, a portion of it?

How big are the files?

Are you looking to remove the [line] only if the expiration date is met or exceeded?

LOL, yep, exactly what I was thinking… Hence, enough for him to start with…

the file is around 3K lines but it’s not all of em probably 100ish to update monthly…
i forgot to mention that i wanna create a seperate file which includes the one with the lines in it.

Are you looking to remove the [line] only if the expiration date is met or exceeded?

^ Yes.

So, are you looking on working on this code yourself, or to be handed a working example.

If the later, let me know and I will move this to the freelancer said, which I would be more than happy to handle actually. If the former,

Take the file into memory.
Split the file into and array.
Ernie gave guidelines for what to do from there.

You should seriously consider using a proper database instead of fumbling with text files. If you’re afraid of full-blown database systems like MySQL, there’s always SQLite which stores all data in a single file and doesn’t require any admin work.

Adding and removing data from plaintext files is a nightmare. Not only does it require a lot of complicated code (as you can see). It’s also extremely error-prone. If anything goes wrong (like PHP timeouts while writing, concurrent processes tramping each other or application bugs), you’ll quickly end up with a complete mess of broken data.

If you had a proper database, you wouldn’t even have to delete any lines. You could just exclude them when you select the current data.

Sponsor our Newsletter | Privacy Policy | Terms of Service