Forum Moderators: coopster

Message Too Old, No Replies

eregi to preg match

         

zozzen

9:22 am on Oct 9, 2010 (gmt 0)

10+ Year Member



Hi I got a serious trouble at turning my eregi code into preg match.

My site has introduced a censorshop on the use of some words. It was implemented by eregi. However, recently my server has been upgraded to php 5.3 and the code doesn't work anymore.

While I've successfully replaced eregi_replace with preg_replace, this eregi syntax doesn't seem to work, possibly due to the use of Trim() in the code.

Please teach me what i should do. Thanks a lot.

$fh = fopen("bannedword.txt","r");

while($word = fgets($fh)) {

if (eregi(trim($word), $output))
{
echo "Sorry. Nothing shows.";
exit;
}

}

enigma1

12:28 pm on Oct 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You will have to change the contents of the bannedword.txt -each line- for the conversion, it is not only the function name. Finally you will replace it with:

if( preg_match($word, $output)) ......

zozzen

2:57 pm on Oct 9, 2010 (gmt 0)

10+ Year Member



enigma,
it doesn't seem to work yet.

in my bannedword.txt,
I've tried to make the text like this:
/fuxk/
/shxt/
/damn/

and it keeps showing the message like "no ending delimiter". Do you know what happens? thanks a lot...

enigma1

3:12 pm on Oct 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could be the line endings in your file. Can you use a plain text editor like notepad to edit/create a test file? Or setup the auto_detect_line_endings at the beginning of your script

ini_set('auto_detect_line_endings', true);

see if it helps

Matthew1980

5:00 pm on Oct 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all.

Guessing as this is a swear filter the words in the file need to have the correct regex with them so that it works in the function.

/^pattern$/

Personally substr_replace may be a better option...

Definately look at regex..

Cheers
MRb

rocknbil

12:21 am on Oct 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, M's pattern will only match if the word begins (^) and ends($) with $pattern, which may or may not be what you want depending on how you're accessing "$word." But it's the right idea - you need delimiters for the regex, represented by the /'s, which can be any character really.


if( preg_match("/$word/", $output)) ......

if( preg_match("#$word#", $output)) ......

if( preg_match("~$word~", $output)) ......

zozzen

6:32 am on Oct 11, 2010 (gmt 0)

10+ Year Member



Hi, still not sure how it works out but ...it really works!

I've followed matthew's suggestion to examine the regex, tried to remove all possible weird symobl as suggested by enigma1. And following rocknbil's suggestion and adding #...# , things suddenly work without problems anymore!

I really appreciate your help. Thanks a lot!

--------------------

$fh = fopen("banned.txt","r");

while($word = fgets($fh)) {

if( preg_match("#$word#", $output))
{
echo "No results!";
xit;
}

}

-------------
banned.txt (just for example, the actual example doesn't contain any strings like this :> )
-------------
shxt
fxxk
damn
my_god
I_love_you