Forum Moderators: coopster

Message Too Old, No Replies

Help with a for loop!

         

dreamcatcher

6:47 pm on Jun 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys,

I`m trying to implement a bad word filter for a guestbook. I`ve stored the words in a database, now I need to loop through them and filter out unwanted words.

I`ve created a function as follows:

function badWordFilter() {

global $comments;

$query = "SELECT * FROM mg_badwords";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

for ($n=0; $n<sizeof($row); $n++)

{

$comments = eregi_replace($row['mg_badword'][$n], $row['mg_newword'][$n], $comments);

}

return $comments;

}

Problem I have is only the first row is working. I assume its something to do with the for loop?

Thanks.

:)

lorax

6:54 pm on Jun 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Move the mysql_fetch_array() function to within the loop and use the number of records returned for the counter.

dreamcatcher

7:22 pm on Jun 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you give me an example? I`ve put the mysql_fetch_array into the loop, but I`m unsure of the rest. I haven`t used for loops before.

dreamcatcher

8:49 pm on Jun 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Its ok, I got it. Finally decided on the following:

function badWordFilter() {

global $comments;

$query = "SELECT * FROM mg_badwords";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result))

{

$bad[] = $row['mg_badword'];
$good[] = $row['mg_newword'];

}

for ($n=0; $n<sizeof($bad); $n++)

{

$comments = eregi_replace($bad[$n], $good[$n], $comments);

}

return $comments;

}

Works like a charm.

:)

lorax

2:08 am on Jun 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry I didn't get back to you sooner dreamcatcher - but I'm glad you got it figured out.

dreamcatcher

5:45 am on Jun 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry I didn't get back to you sooner dreamcatcher - but I'm glad you got it figured out.

No problem at all lorax. I`m kind of glad I figured it out myself, means my learning is coming along ok, if even sometimes I do do things the long way rather than the short way.

Thanks again.

:)

jamie

9:58 am on Jun 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi dreamcatcher,

something i read yesterday whilst looking for ways to speed up code is to take the sizeof() out of the for loop, and define it beforehand. that means you are then only looping and comparing a constant instead of the script having to find the sizeof() every time you loop.

depending on how big your list of bad words is, it could help quite a bit

e.g.

$a = sizeof($row)

for ($n=0; $n < $a; $n++)

olwen

10:06 am on Jun 27, 2003 (gmt 0)

10+ Year Member



In general it's better to refrain from repeatedly doing the same calculation. It's a lesson I learnet long ago when computers were much slower than they are now, and I used COBOL. Well-optimised code becomes somewhat of a habit. Many techniques work in most languages.

dreamcatcher

10:01 pm on Jun 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the additional information. :)

vincevincevince

3:50 am on Jun 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In general it's better to refrain from repeatedly doing the same calculation

although i'm not sure about the php, many of the better C++ compilers will change this for you automatically when you compile...

olwen

4:30 am on Jun 28, 2003 (gmt 0)

10+ Year Member



I'm not sure about PHP either, although I though it was more an interpreter than compiler. THere were no optimising compilers in the 1970s when I learnt this I think. But it is still not bad proctise to write good code.

I do remember a story a C++ programmer told me about writing some code to test timings. The optimiser did the job so well it removed all the delays deliberately inserted.

vincevincevince

5:43 pm on Jun 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hehe, good story :-)

lorax

1:20 am on Jun 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



C++ is compiled - PHP is not. PHP is a script language pure and simple.

ShawnR

2:00 am on Jun 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



lorax, I'd agree that it is safest to think about it that way, but I don't think it is that pure and simple. PHP 4 does some optimisation even though it is not a compiled language. Another example of this 'not strictly interpreted line by line' behaviour is that it has run-time binding of functions so you can call a function before you define/declare it. With some of the things coming out of Zend the line between compiled vs interpretted is even more blurry when it comes to php.

With perl, even more optimisation happens... Perl is thought of as an interpreted scripting language, but the whole script is read in and converted to an internal virtual machine code (with lots of optimisation along the way) before being executed.

Having said that, I agree with lorax that it is a good idea to write code which is efficient no matter how good the compiler or interpreter's optimisation algorithms are. i.e. Just think of PHP as interpretted, pure and simple, and any further optimisation the zend engine gives you will be a bonus.

Shawn