01-28-2008, 02:53 PM
|
#3 (permalink)
|
|
The Frequenter
Join Date: Apr 2005
Location: South UK
Posts: 482
Thanks: 51
|
Hi Gareth,
Quote:
Originally Posted by Gareth
I would be able to do this outside of the function, easily, with mysql_fetch_array, but how would i do this in the function?
|
I would select them outside of the function then pass the array of bad words to your cuss_filter() function.
For example:
PHP Code:
<?php
function cuss_filter($string, $bad_words) {
foreach ($bad_words as $cuss) {
if (stristr(trim($string),$cuss)) {
$length = strlen($cuss);
for ($i = 1; $i <= $length; $i++) {
$stars .= "*";
}
$string = eregi_replace($cuss,$stars,trim($string));
$stars = "";
}
}
return $string;
}
?>
<?php
// Select all of the bad words from the sbb_badwords table
$badwords_q = "SELECT word FROM sbb_badwords";
$badwords_r = mysql_query($badwords_q);
// Loop through each badword...
foreach ($badwords_a = mysql_fetch_array($badwords_r))
{
//...and stuff each one into an array
$badWordList[] = $badwords_a['word'];
}
/*
This will give you an array that looks something like:
Array
(
[0] => list
[1] => of
[2] => bad
[3] => words
[4] => here
)
*/
$content = "I have bad language";
// Pass the text and array of bad words to the cuss_filter() function
echo cuss_filter($content, $badWordList);
Something like that would probably work.
Alternativly, if you really want the mysql() bits inside the function you can just move it to the top of your function. You will need to remember to either use the global keyword for your database connection so that it can be used inside your function or pass your database connection to your function.
Eg:
PHP Code:
// Using global
function cuss_filter($string)
{
global $db;
// ...rest of your code
}
PHP Code:
// Passing the database connection
function cuss_filter($string, &$db)
{
// ...rest of your code
}
cuss_filter($content, $db);
Alan
|
|
|