Koum Posted August 17, 2007 Share Posted August 17, 2007 Ok so basically, i want to create a PHP Script that would go through a list inside a text file and ignore all of one kind of value and print all of another? e.g. lets say i had a text file that had in it: koum test test test test koum i would want it to go through that text file, ignore all of the test's and only print or echo the koum's Thanks for any help given - Koum Quote Link to comment https://forums.phpfreaks.com/topic/65470-solved-php-finding-text/ Share on other sites More sharing options...
wildteen88 Posted August 17, 2007 Share Posted August 17, 2007 You'd just use an if statment that will check to see if the current lines value does not equal to test then print the line, eg: $lines = file(file.ext); foreach($lines as $line => $line_value) { if($line_value != 'test') { echo $line; } } If the lines contains more than one word then you might have to use eregi or strpos which will check to see if the word test is used within the line. Quote Link to comment https://forums.phpfreaks.com/topic/65470-solved-php-finding-text/#findComment-326903 Share on other sites More sharing options...
Koum Posted August 17, 2007 Author Share Posted August 17, 2007 what if there was more values though, so koum test fred ben etc, print all accept test. so far though, i think i can work it out so going to set this as solved thanks -Koum Quote Link to comment https://forums.phpfreaks.com/topic/65470-solved-php-finding-text/#findComment-326905 Share on other sites More sharing options...
Koum Posted August 17, 2007 Author Share Posted August 17, 2007 ok after doing what you said before, it wasnt working i then adding quotion marks around the file name and it half works lol <?php /** * @author |TD| Koum * @copyright 2007 */ $lines = file('file.txt'); foreach($lines as $line => $line_value) { if($line_value = 'test') { echo $line; } } ?> problem is it prints the number of lines inside the text file, compared to what i want it to do Quote Link to comment https://forums.phpfreaks.com/topic/65470-solved-php-finding-text/#findComment-326985 Share on other sites More sharing options...
wildteen88 Posted August 17, 2007 Share Posted August 17, 2007 oops, sorry I did a typo. It should be: $lines = file('file.txt'); foreach($lines as $line => $line_value) { if(trim($line_value) != 'test') { echo $line_value . '<br />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/65470-solved-php-finding-text/#findComment-326986 Share on other sites More sharing options...
Koum Posted August 17, 2007 Author Share Posted August 17, 2007 lol cheers, as u can see im not too good in PHP only just started out ^^ - Koum Quote Link to comment https://forums.phpfreaks.com/topic/65470-solved-php-finding-text/#findComment-326991 Share on other sites More sharing options...
wildteen88 Posted August 17, 2007 Share Posted August 17, 2007 Just as a side note if you want to check against multiple words a good way would be to do this: $lines = file('file.txt'); // list bad words in array $bad_words = array('test', 'ben'); foreach($lines as $line => $line_value) { $line_value = trim($line_value); // use in_array to check if the word on the current line is a bad word. // if it is we wont display it. if(!in_array($line_value, $bad_words)) { echo $line_value . '<br />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/65470-solved-php-finding-text/#findComment-327020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.