Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP language filter, trouble opeing txt file to read form

Posted on 2004-09-22
10
Medium Priority
?
233 Views
Last Modified: 2013-12-13
This was a tutorial on another site and cant seem to get this working.  

<?php

$string = @file("c:\indigoperl\apache\htdocs\string.txt");

function language_filter($string) {

    $obscenities = @file("c:\indigoperl\apache\htdocs\foul.txt");

    foreach ($obscenities as $curse_word) {
        if (stristr(trim($string),$curse_word)) {
            $length = strlen($curse_word);
            for ($i = 1; $i <= $length; $i++) {
                $stars .= "*";
            }
            $string = eregi_replace($curse_word,$stars,trim($string));
            $stars = "";
        }
    }
    return $string;

}
print language_filter($string);


?>

basically all I am trying to do is read from string.txt and check that array against foul.txt for any bad words.  I have tried numerous things but nothing seems  to work.  The only time that it worked was when the foul words was actually in an array in the code.  With the present code all I am getting is 'Array' written to the screen.  I know it is a simple fix.  Thanks
0
Comment
Question by:mnoel76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 12124322
Possibly you should change the first reading of the file string.txt
$string = @file("c:\indigoperl\apache\htdocs\string.txt");
==> change to ==>
$string = implode('',@file("c:\indigoperl\apache\htdocs\string.txt"));

You seem to be handling $string as an scalar, while file returns an array.

You also should add error_reporting(E_ALL); as the first line of your code.

regards

-r-
0
 

Author Comment

by:mnoel76
ID: 12124422
Roonan

string.txt is the file that I am reading into a string.  It is a sentence to check to see if there are obscene words.  foul.txt which has one word per line.  loops through each line of the foul.txt to see it string.txt has another bad words.
I will try what you have suggested.
0
 

Author Comment

by:mnoel76
ID: 12124452
Roonan we seem to be having some success but it is not catching the second obscene word.

here is the present output
Notice: Undefined variable: stars in C:\indigoperl\apache\htdocs\language_filter.php on line 15
**** words are not always foul in their language.

And what is the purpose of "You also should add error_reporting(E_ALL); as the first line of your code."

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Accepted Solution

by:
Roonaan earned 400 total points
ID: 12124515
The purpose of error_reporting(E_ALL) is to force php to show all types of warnings/notices.

As with the $stars error, I should change the inner loop:
foreach ($obscenities as $curse_word) {
        if (stristr(trim($string),$curse_word)) {
            $length = strlen($curse_word);
            for ($i = 1; $i <= $length; $i++) {
                $stars .= "*";
            }
            $string = eregi_replace($curse_word,$stars,trim($string));
            $stars = "";
        }
    }

Change it into:

foreach ($obscenities as $curse_word) {
        if (stristr(trim($string),$curse_word)) {
            $stars = str_repeat('*', strlen($curse_word));
            $string = eregi_replace($curse_word,$stars,trim($string));
        }
    }


Regards

-r-
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12124537
Some explenation on the single sentence in file question.

File will not recognize it is just one line and still will return an array. The only thing is the array contains only one item, your single sentence.

I expect in a next phase you will not load $string from file but fill it with some kind of dynamic content, but in this phase imploding was necessairy.

-r-
0
 

Author Comment

by:mnoel76
ID: 12124869
beautiful.  thanks for your help Roonan!!!
0
 

Author Comment

by:mnoel76
ID: 12124897
Roonan if I were to load dynamic content want would I use instead?
0
 

Author Comment

by:mnoel76
ID: 12124978
Actuually Roonan,  dont worry about the above question.  I just realized that I had hard coded the obscene words into my php file.  So we are not out of the woods yet.  I still am having problems opening the foul.txt file and having it star out all the obscene words.  

$obscenities = @file("c:\indigoperl\apache\htdocs\foul.txt");  right now it is only reading the last word in the foul.txt
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12125050
Only the last word? That's strange. I read the code about 4 times and couldn't find nothing odd.

-r-
0
 

Author Comment

by:mnoel76
ID: 12125165
<?php
error_reporting(E_ALL);

$string = implode('',@file("c:\indigoperl\apache\htdocs\string.txt"));

function language_filter($string) {

    $obscenities = @file("c:\indigoperl\apache\htdocs\foul.txt");


    foreach ($obscenities as $curse_word) {
        if (stristr(trim($string),$curse_word)) {
            $stars = str_repeat('*', strlen($curse_word));
            $string = eregi_replace($curse_word,$stars,trim($string));
        }
   

    }
    return $string;

}
print language_filter($string);

this is the code that I am using.  does it matter what the txt file looks like??
word1
word2
word3
in this format to properly read the words.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question