Jump to content

[SOLVED] PHP Finding text


Koum

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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 />';
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.