Find the answer to your Linux question:
Results 1 to 6 of 6
PHP Yet another question. I want to scan some form input and if bad / rude words are found then return an error. Any opinions on what to use. There ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User
    Join Date
    Jan 2003
    Location
    Cardiff, Wales
    Posts
    478

    content filter


    PHP
    Yet another question. I want to scan some form input and if bad / rude words are found then return an error.

    Any opinions on what to use. There are various sub string functions and ereg anyone have any ideas on the best solution. I would probably make a dbase table of the bad words and then load these into an array. I thought maybe I should construct one big ereg rather than loop through and check lots of single ones. Anyone done this before?
    No trees were harmed during the creation of this message. Its made from a blend of elephant tusk and dolphin meat.

  2. #2
    Linux Engineer Giro's Avatar
    Join Date
    Jul 2003
    Location
    England
    Posts
    1,219
    the best way IMO would be to loop though the bad words with a preg_match and if you get a bad word die();

  3. #3
    Linux User
    Join Date
    Jan 2003
    Location
    Cardiff, Wales
    Posts
    478

    thanks

    thanks giro. I looked at the PHP manual page for that function
    http://uk2.php.net/preg_match

    and that suggests using strpos() or strstr() as they will be faster if you want to find out if one string is inside another.

    I am going to try both of these and time them. I'll post my code and results back for comments or in case anyone's interested.
    No trees were harmed during the creation of this message. Its made from a blend of elephant tusk and dolphin meat.

  4. #4
    Linux User
    Join Date
    Jan 2003
    Location
    Cardiff, Wales
    Posts
    478

    ereg

    Unfortunately I require case insensitive searching on PHP 4 so I tried strpos, strstr, stristr, ereg and eregi. All these gave me pretty much the same speed values. my functions for gathering microseconds took longer than the script did. they all perform really well - even with long documents.

    Code:
    <html>
    <head>
    <title>Content Filter Test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    
    
    Content Filter Test</p>
    
    <?php
    if &#40;$Submit == "Submit"&#41; &#123;
    	mysql_pconnect&#40;"localhost", "username", "password"&#41;;
    	mysql_select_db&#40;"dbecommunity"&#41;;
    	$res = mysql_query&#40;"SELECT * FROM tbbadwords"&#41;;
    	
    	$cont = true;
    	$ans = "Everythings ok";
    	while&#40;&#40;$words = mysql_fetch_array&#40;$res&#41;&#41; && &#40;$cont&#41;&#41; &#123;
    		if &#40;! eregi&#40;" " . $words&#91;"badword"&#93; . " ", " " . $message . " "&#41;&#41; &#123;
    			// do nothing
    		&#125; else &#123;
    			$cont = false;
    			$ans = "Bad word found.";
    		&#125;
    	&#125;
    	echo&#40;"
    
    " . $ans . "</P>"&#41;;
    
    &#125;
    ?>
    <form name="form1" method="post" action="ereg.php">
    
    
        <textarea name="message" cols="50" rows="20" id="message"><?php echo&#40;$message&#41;; ?></textarea>
    
    
        <input type="text" name="textfield">
    
    
    
    
        <input type="Submit" name="Submit" value="Submit">
    
    
    </form>
    
    
    
    </body>
    </html>
    gonna try preg_match now just for fun. thanks giro
    No trees were harmed during the creation of this message. Its made from a blend of elephant tusk and dolphin meat.

  5. #5
    Linux User
    Join Date
    Jan 2003
    Location
    Cardiff, Wales
    Posts
    478

    preg_match

    this seems even better giro
    Code:
    if &#40;! preg_match&#40;"/\b" . $words&#91;"badword"&#93; . "\b/i", $message&#41;&#41; &#123;
    sorry to have doubted you!
    No trees were harmed during the creation of this message. Its made from a blend of elephant tusk and dolphin meat.

  6. #6
    Linux Engineer Giro's Avatar
    Join Date
    Jul 2003
    Location
    England
    Posts
    1,219
    preg has a big differnce it uses perl regex's they are quick and powerfull you can do some cool things with regex's like get head aches ereg uses php's regex engine IIRC which is not as powerfull as perl's but why reinvent the wheel.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •