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 03-10-2010, 04:16 AM   #1 (permalink)
The Contributor
 
Tim Dobson's Avatar
 
Join Date: Feb 2010
Posts: 69
Thanks: 16
Tim Dobson is on a distinguished road
Default word filter help

I have this code:
PHP Code:
<?php
                    
$myFile 
"quotes.txt";
$fh fopen($myFile'r');
$ToFilter fread($fhfilesize($myFile));
fclose($fh);
function 
LangFilter($ToFilter)

{

   
// Open the foul.txt for extracting the foul words

   
$Foul = @file("foul.txt");

   
// Loop through the foul words

   
foreach ($Foul as $FoulWord)

   {

      
// Store the current foul word in a variable

      
$FoulWord trim($FoulWord);

      
// If there's a match for the fould world inside the string

      
if (preg_match("/".$FoulWord."/i"$ToFilter))

      {

         
// Get the word length, so that we know how many characters

         // we need to replace with asterisks

         
$WordLength strlen($FoulWord);

         
// Loop through the word's characters

         
for ($i 1$i <= $WordLength$i++)

         {

            
// Replace the characters of the foul word with * (asterisks)

            
$RepChar .= "*";

         }

         
// Replace the dirty string with the filtered string

         
$ToFilter eregi_replace($FoulWord$RepChartrim($ToFilter));

         
$RepChar "";

      }

   }

   
// Return the new string

   
return $ToFilter;

}
echo 
$ToFilter;
?>
quotes.txt is data that is to be loaded to the page and foul.txt is a text file containing all words that are to be filtered. Although its not working... obviously its not right could somone plz correct me, new to php!
Tim Dobson is offline  
Reply With Quote
Old 03-10-2010, 06:27 AM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Usually I write all my comments as, well, comments, but I'll go over a few things and just plaster the code below for ya.

- First of all, not sure if the code you pasted is what you're actually testing, but when you echo $ToFilter at the bottom, you haven't ever actually run your function on it.

- Unless you have a specific need for fread, file_get_contents is quicker and does the same thing in one line instead of three. PHP is like a large city, there's 20 routes to get anywhere, and there's usually a few that are better than others.

- Don't use error suppression unless you have no other choice. Especially in your case, because the function will continue to run having no data to work with.

- str_replace is much faster than preg_replace. Use it when you can. Also, you're spending time looking for the words, and then looking for them again to perform the replace. That's like going to two bathrooms for one shave.

- Loops, again, should only be performed when there isn't a better method (though I understand being new you may not have learned them all yet, just letting you know!). PHP has an extensive list of string functions, and I'll show you one of them that negates the need for for()

- When all else fails debugging, don't be afraid to drink a few beers and work on your car. It's much more rewarding than this crap anyways... or maybe that's just because I've been working 14 hour days for the past week with pain in the ass clients...

- Oh yeah. I try not to change variables when helping somebody with code, but CamelCase drives me batty. Sorry.

php Code:
<?php

$input = 'quotes.txt';
$wordsfile = 'words.txt';

$string = file_get_contents( $input );

echo LangFilter( $string, $wordsfile );


function LangFilter( $string = '', $wf ) {

    if ( empty( $string ) || ! file_exists( $wf ) )
        return FALSE;

    $words = file( $wf );

    foreach ( $words as $search ) {
        $search = trim( $search );
        $replace = str_repeat( '*', strlen( $search ) ); // No loopy inside loopy
        $string = str_ireplace( $search, $replace, $string );
    }

    return $string;

}
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Tim Dobson (03-10-2010)
Old 03-10-2010, 03:47 PM   #3 (permalink)
The Contributor
 
Tim Dobson's Avatar
 
Join Date: Feb 2010
Posts: 69
Thanks: 16
Tim Dobson is on a distinguished road
Default

delayedinsanity your to good for this. I will put a request in to the queen for you to recive a medal so between now and 1000 years you might recive one. LOL thanks :) you put in so much effort to help people :P
Tim Dobson is offline  
Reply With Quote
Old 03-10-2010, 03:50 PM   #4 (permalink)
The Contributor
 
Tim Dobson's Avatar
 
Join Date: Feb 2010
Posts: 69
Thanks: 16
Tim Dobson is on a distinguished road
Default

delayedinsanity your to good for this. I will put a request in to the queen for you to recive a medal so between now and 1000 years you might recive one. LOL thanks :) you put in so much effort to help people :P

Just one more thing... its not important but what if i just wanted to keep the first letter and the last letter in there and replace all the others? sounds simple enough and what you said made sense i actually had a script similar but lost it and couldent find it again... think it flew off somewhere to the east. Trying to get my head around this php and its coming along slowly with all the help im getting from here.
Tim Dobson is offline  
Reply With Quote
Old 03-10-2010, 05:24 PM   #5 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

There's a queen? Wait, are you talking about my wife? Depends on the time of month whether or not she likes being called that...

Anywho, it is fairly simple. Since you have the stop word you're searching for, you could easily use substr() to pull the first letter ( 0, 1 ) from the word, and the last ( -1 ), then use those on either end of $replace and reduce the str_repeat function by -2. This would take very little effort, and you'd still be zipping along with str_replace().

There's only one problem with this solution though. How do you maintain case if one of your words is found at the start of a sentence?

Okay, so str_replace isn't your best option anymore. Now preg_replace is your friend (use preg, forget ereg exists because PCRE > POSIX and it's deprecated anyways). Regular expressions frighten a lot of people but they really shouldn't - you can do generally useful things with the simplest of expressions most of the time.

php Code:
// The new and improved loop

            foreach ( $words as $word ) {
        $word = trim( $word );

        $pattern = '~(' . substr_replace( $word, ')' . substr( $word, 1, -1 ) . '(', 1, -1 ) . ')~^i';
        $replace = '$1' . str_repeat( '*', strlen( $word )-2 ) . '$2';

        $string = preg_replace( $pattern, $replace, $string );
    }

Simple enough, right? All we're doing is surrounding the first and last letter of the word with parentheses so that we can capture them and reuse them in the replacement string. So we're taking a word such as "lorem" and making it "(lorem)". Then using substr_replace() with a nested substr(), we're pulling "orem" out, and replacing it with ")orem(" so that what we're left with is "(l)ore(m)". You could alternatively just substr the first and last letter off into variables, then use them to do something such as;

php Code:
$first = substr( $word, 0, 1 );
$last = substr( $word, -1 );
$middle = substr( $word, 1, -1 );

$replace = "({$first}){$middle}({$last})" // without the regular expression delimiters and i modifier
 

It all depends on your personal preference. I prefer to avoid assigning variables I'm only going to use once. Other people find it more readable, and whatever works best for you is what you should do.

Okay. Ever heard of Simple:Press Forums? The nitwits who programmed it used tables and didn't even bother to assign unique CSS identifiers to each one. If you could just fix that for me, we'll be square. :D
delayedinsanity is offline  
Reply With Quote
Old 03-10-2010, 05:28 PM   #6 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

*shakes his fist at vBulletin*

Stop messing with my formatting, forums!
delayedinsanity 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
How Can read and search a word from pdf file nanhe Advanced PHP Programming 5 10-15-2009 04:31 AM
Spell Check / Words similar to word - MYSQL russellharrower Advanced PHP Programming 5 08-10-2009 12:43 PM
How to go to a particular page when i click on a particular word? sandhyagupta Absolute Beginners 18 07-03-2009 02:43 PM
Use one word... Alan @ CIT The Lounge 7 03-08-2008 08:09 AM
how to filter textboxes or fields from xss sarmenhb Absolute Beginners 5 02-18-2008 09:58 PM


All times are GMT. The time now is 04:08 AM.

 
     

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