File I/O

There are a selection of functions from the bottom up, separated by horizontal rules.


I created a file named lyric.txt, copied some lyrics. I also use a variable named $lyric in some places.

How many times does a particular word show up in my text file lyric.txt?
(The function in fileio.php references the variable $lyric in that same file and the substring to search for, $testedWord, in this file. A form saves the entered data to a file and then retrieves it here.)

Enter a word or string (case sensitive) to learn how many times it appears in the lyrics. Suggestions are 'you' 'made' 'when' 'When'.
   
The word 'you' appears 11 times.


Tested these functions. They worked. Displaying the lyrics here for convenience so the function above can be tested.
// readFileIntoString(); // no line breaks
// readFileLineByLine(); // as in original file with line breaks

THE STORY
by Brandi Carlile

All of these lines across my face
Tell you the story of who I am
So many stories of where I've been
And how I got to where I am
But these stories don't mean anything
When you've got no one to tell them to
It's true, I was made for you

I climbed across the mountain tops
Swam all across the ocean blue
I crossed all the lines and I broke all the rules
But baby, I broke them all for you
Oh, because even when I was flat broke
You made me feel like a million bucks
You do, and I was made for you

You see the smile that's on my mouth
It's hiding the words that don't come out
And all of my friends who think that I'm blessed
They don't know my head is a mess
No, they don't know who I really am
And they don't know what I've been through like you do
And I was made for you

All of these lines across my face
Tell you the story of who I am
So many stories of where I've been
And how I got to where I am
Oh, but these stories don't mean anything
When you've got no one to tell them to
It's true, I was made for you

Oh, yeah, well it's true that I was made for you


Summarizing a few filesystem functions on lyric.txt:

Number of characters: 1092
File Last Accessed: March 20 2024 16:39:17.
Content Last Modified: March 29 2022 03:32:42.
File Last Modified: March 20 2024 19:53:25.

I find it odd that they would put a period after the timestamp.


I understood your CSV example and duplicated it with my file jlCsv.txt.
"Create an array and save it to to a file as a comma separated list (fputcsv) when writing, and turn the comma separated list back into an array (fgetcsv) when reading."
I then manually created the bands.csv file and wrote the following function to try to work towards "reading an actual multi-record CSV file and turning it into multiple arrays with column headers being the keys." Interestingly the first function produces an array and the second simply echos the original data, but I thought it was cool so I left it for now.

Array ( [0] => Favorite Bands [1] => Favorite Musician [2] => Notable Mix )
Pink Floyd,David Gilmour,Echoes Live at Pompeii
Array ( [0] => Eagles [1] => Glenn Frey [2] => Hotel California )
Led Zeppelin,Jimmy Page,Kashmir
Array ( [0] => Beatles [1] => George Harrison [2] => While My Guitar Gently Weeps )


©2022 Darlene McCormick • Web page by me