mnoel76
asked on
PHP language filter, trouble opeing txt file to read form
This was a tutorial on another site and cant seem to get this working.
<?php
$string = @file("c:\indigoperl\apach e\htdocs\s tring.txt" );
function language_filter($string) {
$obscenities = @file("c:\indigoperl\apach e\htdocs\f oul.txt");
foreach ($obscenities as $curse_word) {
if (stristr(trim($string),$cu rse_word)) {
$length = strlen($curse_word);
for ($i = 1; $i <= $length; $i++) {
$stars .= "*";
}
$string = eregi_replace($curse_word, $stars,tri m($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
<?php
$string = @file("c:\indigoperl\apach
function language_filter($string) {
$obscenities = @file("c:\indigoperl\apach
foreach ($obscenities as $curse_word) {
if (stristr(trim($string),$cu
$length = strlen($curse_word);
for ($i = 1; $i <= $length; $i++) {
$stars .= "*";
}
$string = eregi_replace($curse_word,
$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
ASKER
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.
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.
ASKER
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\htdoc s\language _filter.ph p 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."
here is the present output
Notice: Undefined variable: stars in C:\indigoperl\apache\htdoc
**** 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."
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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-
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-
ASKER
beautiful. thanks for your help Roonan!!!
ASKER
Roonan if I were to load dynamic content want would I use instead?
ASKER
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\apach e\htdocs\f oul.txt"); right now it is only reading the last word in the foul.txt
$obscenities = @file("c:\indigoperl\apach
Only the last word? That's strange. I read the code about 4 times and couldn't find nothing odd.
-r-
-r-
ASKER
<?php
error_reporting(E_ALL);
$string = implode('',@file("c:\indig operl\apac he\htdocs\ string.txt "));
function language_filter($string) {
$obscenities = @file("c:\indigoperl\apach e\htdocs\f oul.txt");
foreach ($obscenities as $curse_word) {
if (stristr(trim($string),$cu rse_word)) {
$stars = str_repeat('*', strlen($curse_word));
$string = eregi_replace($curse_word, $stars,tri m($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.
error_reporting(E_ALL);
$string = implode('',@file("c:\indig
function language_filter($string) {
$obscenities = @file("c:\indigoperl\apach
foreach ($obscenities as $curse_word) {
if (stristr(trim($string),$cu
$stars = str_repeat('*', strlen($curse_word));
$string = eregi_replace($curse_word,
}
}
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.
$string = @file("c:\indigoperl\apach
==> change to ==>
$string = implode('',@file("c:\indig
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-