TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-28-2008, 01:29 PM   #1 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default 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
Gareth is offline  
Reply With Quote
Old 01-28-2008, 01:53 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Do you mean something like the following?

sql Code:
SELECT
    word
FROM
    sbb_badwords
WHERE
    word
IN
    ('git', 'bugger', 'tart')
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-28-2008, 02:53 PM   #3 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Hi Gareth,

Quote:
Originally Posted by Gareth View Post
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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-29-2008, 12:31 PM   #4 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

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
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 01-29-2008, 03:52 PM   #5 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

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).
Gareth is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:11 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design