read a line from txt file

Hi there, im making a small function to collect txt files content that is filterd by markdown.
i need to change my code to add a date and time format to the first line (line 0)

my problem is i dont know how to read the date and time from the first line
then use that date and time to sort the files by date time order
then i need to ignore the first line so i can get the content

any help would be great , heres my function…

[php]
function get_media($media_cat, $display_recent) {
global $Parsedown, $media_id;
if (isset($media_id)) {
if ($media_id == $media_cat) {
$display_recent = null;
}
}
$directory = MEDIA_BASEDIR.$media_cat.’/’;
$files = array();
$files = glob($directory.’*.txt’);
usort($files, function($x, $y) {
return filectime($x) < filectime($y);
});
$recent_content = array_slice($files, 0, $display_recent);
foreach ($recent_content as $media) {
$content = file_get_contents($media);
$media_title = basename($media);
$media_title = basename($media, ‘.txt’);
if (file_exists($media)) {
$date_time = date (‘F d Y H:i:s’, filectime($media));
}
THEMEMEDIATOP($media_title, $media_cat);
echo $Parsedown->text($content);
THEMEMEDIABOTTOM($date_time);
}
}
[/php]
i obviousley would have to remove/change
[php]
usort($files, function($x, $y) {
return filectime($x) < filectime($y);
});
[/php]
and the date in the THEMEMEDIABOTTOM function would be the top line of the collected file
and $content would have to be changed to ignore the first line.

im not useing a database because my aim is to be able to upload my txt files to a directory and thats it
then its added to the top as newest first and all i have to do in my text file is add the date and time at the top, should i edit my content i can quickly edit the date and time to bring it to the top or just leave it as is even after editing it

thanks

First thing, don’t name functions in all capital letters, it is bad practice.

If it is always on the first line, you should easily be able to skip line 0.

First thing, don't name functions in all capital letters, it is bad practice.
noted, i will update that as soon as i get chance..
If it is always on the first line, you should easily be able to skip line 0.
yes but how do i do it? i dont know how to

read the date and time to use that date and time to sort the list
then how do i ignore line 0?

Post an example file, doesn’t have to be real data but the format should be the same.

and the date in the THEMEMEDIABOTTOM function would be the top line of the collected file and $content would have to be changed to ignore the first line.

Made me think you wanted to toss the timestamp.

Like I said, I am a fan of regex, but string manipulation also comes in handy.

Hi thankyou for the reply, not toss it just change it the the date and time in the file rather than useing filectime

my txt file will format lile this

[php]1/3/2017 8:20am
And the rest is txt to be filterd with markdown
Some more txt
And more [/php]

Line 1 would be 0
$content would be all the rest after date and time
Thanks

Sorry, took me a while to get back to this!

[php]
$file = ‘somefile.txt’;
$lines = file($file);
echo $lines[0];
[/php]

That would give you the very first line of the file. I think that is what you are asking???

no worries dude im realy busy with work at the mo anyway
this is just a hobby of mine, cheers for the reply, i know how to do that its the sorting function to get the date time from line 0 that ive messed up on, i cant get it to work
i have tried to do it myself and only came up with this:

[php]function get_media($media_cat, $display_recent) {
global $Parsedown, $media_id;
if (!empty($media_id)) {
if ($media_id == $media_cat) {
$display_recent = null;
}
}
$directory = MEDIA_BASEDIR.$media_cat.’/’;
$files = array();
$files = glob($directory.’*.txt’);
foreach ($files as $file) {
$line = file($file);
$date_time = $line[0];
$date_time = $Parsedown->text($date_time);

	uasort($files, 
	function ($a, $b) {
		global $date_time;
			return strcmp ($a[$date_time], $b[$date_time]);
	});
	
	$recent_content = array_slice($files, 0, $display_recent);
	foreach ($recent_content as $media) {
		$content = file_get_contents($media);
		$media_title = basename($media);
		$media_title = basename($media, '.txt');
		thememediatop($media_title, $media_cat);
		echo $Parsedown->text($content);
		thememediabottom($date_time);
	}
}

}[/php]

i get a error on this line
" String offset cast occurred"

[php]return strcmp ($a[$date_time], $b[$date_time]);[/php]

i know that ive set the code up wrong in what ive just posted but its all i could come up with on my own ive searched all over and i cant work it out

thanks

You are using way to many globals, like any!

I am going to make the assumption that $date_time is the actual datetime? Something like 1/3/2017 8:20am?

And in that case, rather than doing a string comparison, I would do a date comparison.

hi thankyou for the reply, yeah thats what i need
ive just looked on http://php.net/manual/en/function.uasort.php but i cant work out how i can use it with my code…

i dont supose you can give me a example of how i can make it work with my code?

[php]function get_media($media_cat, $display_recent) {
global $Parsedown, $media_id;
if (!empty($media_id)) {
if ($media_id == $media_cat) {
$display_recent = null;
}
}
$directory = MEDIA_BASEDIR.$media_cat.’/’;
$files = array();
$files = glob($directory.’*.txt’);

foreach ($files as $file) {
	$line = file($file);
	$date_time = $line[0];
	$date_time = $Parsedown->text($date_time);
}

	"how can i use the date and time from $date_time to sort the $files"
	
$recent_content = array_slice($files, 0, $display_recent);

foreach ($recent_content as $media) {
	$content = file_get_contents($media);
	$media_title = basename($media);
	$media_title = basename($media, '.txt');
	thememediatop($media_title, $media_cat);
	echo $Parsedown->text($content);
	thememediabottom('');
}

}[/php]

i dont know how to put the two foreach together aswell, when i do everything happens twice over, ive been stuck on this one function for over a week now lol any help is appreciated

thankyou

i have just spent the morning fixing my global mess up… even though im not useing them for anything big i see your point after reading up on it a little deeper, plus i had alot of them all over my site so im glad there gone now… im 100% global free all over my site

updated:

[php]function get_media($media_cat, $display_recent, $Parsedown, $media_id) {
if (!empty($media_id)) {
if ($media_id == $media_cat) {
$display_recent = null;
}
}
$directory = MEDIA_BASEDIR.$media_cat.’/’;
$files = array();
$files = glob($directory.’*.txt’);

foreach ($files as $file) {
	$line = file($file);
	$date_time = $line[0];
	$date_time = $Parsedown->text($date_time);
}


                              "how can i use the date and time from $date_time to sort the $files"
	

$recent_content = array_slice($files, 0, $display_recent);

foreach ($recent_content as $media) {
	$content = file_get_contents($media);
	$media_title = basename($media);
	$media_title = basename($media, '.txt');
	thememediatop($media_title, $media_cat, $media_id);
	echo $Parsedown->text($content);
	thememediabottom('');
}

}[/php]

im still stuck on sorting my files with the date and time i cnt work it out

thanks

i think i have come up with a way of doing it

line0 is now the titles

then the date and time is the filename

and then i just sort the files…

[php]function get_media($media_cat, $display_recent, $Parsedown, $media_id) {
if (!empty($media_id)) {
if ($media_id == $media_cat) {
$display_recent = null;
}
}
$directory = MEDIA_BASEDIR.$media_cat.’/’;
$files = array();
$files = glob($directory.’*.txt’);
usort($files, function($x, $y) {
return $x < $y;
});
$recent_content = array_slice($files, 0, $display_recent);
foreach ($recent_content as $media) {
$line = file($media);
$media_title = $line[0];
$date_time = basename($media);
$date_time = basename($media, ‘.txt’);
$content = file_get_contents($media);
thememediatop($media_title, $media_cat, $media_id);
echo $Parsedown->text($content);
thememediabottom($date_time);
}
}[/php]

how dose it look? im still testing but all seems good

thanks

If it works and does what it should quickly and cleanly.

Rather than echo within the function, i would return an array. I would also make the base directory and this extention a parameter that defaults.
$files = glob($directory.’*.txt’);

That way, it is a bit more portable.

cool, how can i do the base directories and parameter?

i will look into the array retern now
thanks :slight_smile:

i think i have done the print array you mean

[php]function get_media($media_cat, $display_recent, $Parsedown, $media_id) {
if (!empty($media_id)) {
if ($media_id == $media_cat) {
$display_recent = null;
}
}
$directory = MEDIA_BASEDIR.$media_cat.’/’;
$files = array();
$files = glob($directory.’*.txt’);

usort($files, function($x, $y) {
	return $x < $y;
});

$recent_content = array_slice($files, 0, $display_recent);
foreach ($recent_content as $media) {
	
	$line = file($media);
	$media_title = $line[0];
	$date_time = basename($media);
	$date_time = basename($media, '.txt');
	$content = file_get_contents($media);
	$content = substr($content, 5);
	thememediatop($media_title, $media_cat, $media_id);
	print_r ($Parsedown->text($content));
	thememediabottom($date_time);
}

}[/php]

also how can i make this line ignore just line 0 and the hole line not just 5 characters?
[php]$content = substr($content, 5);[/php]

thanks

sorry to spam ive just managed to work out how to remove the title from first line in the content

i did it with str_replace

[php]$content = str_replace($media_title,"",$content);[/php]

[php]function get_media($media_cat, $display_recent, $Parsedown, $media_id) {
if (!empty($media_id)) {
if ($media_id == $media_cat) {
$display_recent = null;
}
}
$directory = MEDIA_BASEDIR.$media_cat.’/’;
$files = array();
$files = glob($directory.’*.txt’);
usort($files, function($x, $y) {
return $x < $y;
});
$recent_content = array_slice($files, 0, $display_recent);
foreach ($recent_content as $media) {
$line = file($media);
$media_title = $line[0];
$date_time = basename($media);
$date_time = basename($media, ‘.txt’);
$content = file_get_contents($media);
$content = str_replace($media_title,"",$content);
thememediatop($media_title, $media_cat, $media_id);
print_r ($Parsedown->text($content));
thememediabottom($date_time);
}
}[/php]
thanks again

Sponsor our Newsletter | Privacy Policy | Terms of Service