1

I have a database which contains the results of logged network traffic, and I'm building a simple log viewer frontend in PHP. I need to check each URL and see if it contains any of the 'bad words' in an external 'badwords.txt' file, then echo which ones, and color the table row red (the easy bit!)

Thus far I have loaded the badwords.txt file into an array, and fetched the URL from the database. Here is the portion of my code where I am trying to get a positively identified 'bad' url. I ideally want to output which badwords were found, but have simplified everything to just try and get it to work for now.

    // Load the badwords file into an array
    $words = file('badwords.txt');

    //$row[3] is the URL fetched from the database
    $testURL = $row[3];

    foreach ($words as $phrase) {
      if (strrpos($testURL, $phrase)) {
        echo "FOUND";
      }
    }

This is not working for me, and never outputs FOUND, even when the url definitely contains a bad word. I have checked that my $words array is populated correctly with all the badwords, and I have checked that the $testURL is not empty etc.

Can anyone help please? :) I'd be really grateful for any assistance - I have read through so many StackExchange posts on similar topics, but none seem to work for my case.

Thank you!

1
  • ^ just use stripos instead Commented Jul 25, 2015 at 12:22

2 Answers 2

5

Words array contains newline symbol, so strpos never works. Remove them

$words = file('badwords.txt', FILE_IGNORE_NEW_LINES);
Sign up to request clarification or add additional context in comments.

1 Comment

I was not aware of this problem, and am VERY grateful to you for having pointed this out. Thank you!!
1

Since strrpos() returns numeric position of needle, and boolean false, so code should be

<?php
 // Load the badwords file into an array
  $words = file('words.txt', FILE_IGNORE_NEW_LINES);

//$row[3] is the URL fetched from the database
$testURL = row[3];

foreach ($words as $phrase) {
  if (strrpos($testURL, $phrase)>-1) {
    echo "FOUND";
  }
}

4 Comments

Thank you for pointing this out about strpos(), and the improved code etc. :) This now correctly returns FOUND for me! Any advice on how I would simply go about determining which words were matched, or am I being pedantic? If it's getting complicated to do that, the current code will provide me with enough to flag the log entry. Thanks again!
Just echo $phrace; after echo "FOUND"; this will echo which word were matched.
Perfect, this was exactly what I was hoping for! It's nearly 2am here, so I can retire in peace now.. Thanks again! :D
If you want you can echo '<br/>'; after echo 'FOUND'; so it prints a line break. it makes text more readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.