Condawg Posted July 18, 2008 Share Posted July 18, 2008 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 More sharing options...
kenrbnsn Posted July 18, 2008 Share Posted July 18, 2008 Here's an example: <?php $inp = file('your.input.file.txt'); foreach ($inp as $word) { $yn = (strlen($word) == 5)?'Yes':'No'; echo $word . ': ' . $yn . '<br>'; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/115354-question-about-flat-files/#findComment-593057 Share on other sites More sharing options...
Condawg Posted July 18, 2008 Author Share Posted July 18, 2008 Here's an example: <?php $inp = file('your.input.file.txt'); foreach ($inp as $word) { $yn = (strlen($word) == 5)?'Yes':'No'; echo $word . ': ' . $yn . '<br>'; } ?> Ken Thank you =D I will try that out. Much appreciation. Link to comment https://forums.phpfreaks.com/topic/115354-question-about-flat-files/#findComment-593059 Share on other sites More sharing options...
kenrbnsn Posted July 18, 2008 Share Posted July 18, 2008 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 Link to comment https://forums.phpfreaks.com/topic/115354-question-about-flat-files/#findComment-593060 Share on other sites More sharing options...
tibberous Posted July 18, 2008 Share Posted July 18, 2008 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. Link to comment https://forums.phpfreaks.com/topic/115354-question-about-flat-files/#findComment-593061 Share on other sites More sharing options...
Condawg Posted July 18, 2008 Author Share Posted July 18, 2008 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 Link to comment https://forums.phpfreaks.com/topic/115354-question-about-flat-files/#findComment-593063 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.