Jump to content

Question About Flat-Files


Condawg

Recommended Posts

Hi =]

Well, I'm working on a script, and I have a file that has a lot of information in it, lots of words, one word a line.

What I would like to do is have a some code that can retrieve all the words from this list, and run a php script to each individual line, and show me the output of each line.

 

I'll give you this example...

Say the file has 5,000 names in it.

The script then takes each name, checks the amount of characters in it.

If it's more then or equal to 5 characters, it displays "Yes" for that word.

If less, it displays "No."

I want to try and have every single one of these archived on a PHP page. Like this...

 

 

Betty          Yes

Bob            No

Jim            No

Connor        Yes

Kenny        Yes

Jane          No

Nate          No

Lenny        Yes

 

Etc...

Please, if you can supply me with a base script to do this, do not make it fit these specifications. That's just an example.

I would like a script that can basically just run a snippet of code for each word in a text file and show results.

How could I accomplish this?

Thank you =]

 

 

Link to comment
https://forums.phpfreaks.com/topic/115354-question-about-flat-files/
Share on other sites

Forgot one thing... You need to trim each word to remove the line terminator.

 

<?php
$inp = file('your.input.file.txt');
foreach ($inp as $word) {
    $yn = (strlen(trim($word)) == 5)?'Yes':'No';
    echo $word . ': ' . $yn . '<br>';
}
?>

 

Ken

Forgot one thing... You need to trim each word to remove the line terminator.

 

<?php
$inp = file('your.input.file.txt');
foreach ($inp as $word) {
    $yn = (strlen(trim($word)) == 5)?'Yes':'No';
    echo $word . ': ' . $yn . '<br>';
}
?>

 

Ken

 

Ahhh. Thank you so much. This file is filled with thousands of words, and scrolling through the results of the test, it was saying "Yes" to random 2 or 3 letter words. Was odd.

Thank you for fixing that =]

 

If you are going to use flat files, but don't require them to be editable by anything but your program, I'd recommend filling out an object or an array, then serializing it and storing it.

 

Sorry, what?

I don't quite know much of the terminology... I was into PHP a year or two ago, then stopped, but decided to try it out again, and I've forgotten a lot.

What would the benifits be, and if you wouldn't mind, could you elabourate?

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.