TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Function with Mysql (http://www.talkphp.com/absolute-beginners/2124-function-mysql.html)

Gareth 01-28-2008 01:29 PM

Function with Mysql
 
Hey guys

I am a real newbie at functions; so I would like some help :)

Basically I have a static version sorted:

PHP Code:

<?php 
function cuss_filter($string) { 
    
$bad_words = array("bad""language"); 
    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
    
    $content 
"I have bad language";
    echo 
cuss_filter($content);

?>

which works fine, but instead of having to write the bad words into the $bad_words, I would like to select the bad words from a database (table name sbb_badwords). 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?

Gareth

Wildhoney 01-28-2008 01:53 PM

Do you mean something like the following?

sql Code:
SELECT
    word
FROM
    sbb_badwords
WHERE
    word
IN
    ('git', 'bugger', 'tart')

Alan @ CIT 01-28-2008 02:53 PM

Hi Gareth,

Quote:

Originally Posted by Gareth (Post 9838)
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

EyeDentify 01-29-2008 12:31 PM

a plain text file would do the trick to if a DB is not needed.

Then read the file into an array with file().
PHP: file - Manual

With one cuss on each line in the text file.

Not optimal but handy.

Good Luck

/EyeDentify

Gareth 01-29-2008 03:52 PM

Thanks, I will definitely have a look at both methods!

I would like to use a database, so I can easily update / edit it online (along with the rest of the acp is based on a database system).


All times are GMT. The time now is 10:58 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0