Forum Moderators: coopster

Message Too Old, No Replies

Warning: strpos() : Empty delimiter (php5)

Empty delimiter (php5)

         

joe da laidoff plumr

5:17 pm on Nov 7, 2008 (gmt 0)

10+ Year Member



I have a form and I am preventing it from being submitted if certain words are used. I am using the php code below to prevent form submissions found on the list of bad words:
-----------------------------------------------------------------

$blockwords="badword 1, badword 2, badword 3,";

if(!empty($blockwords)&&!empty($_POST)){$useBlocks=explode(",",$blockwords);foreach($useBlocks as $blockWord){foreach($_POST as $Name=>$Value){$Value=trim($Value);$Value=strtolower($Value);if(!empty($Value)&&strpos($Value,$blockWord)!==false){exit("--B L O C K E D-- .You used a bad word.");}}}}

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

The code works but it comes with warnings... any idea of how to get rid/fix of the warning? I have googled the warning but all i get are pages with an actual live warning/error
-----------------------------------------------------------------

here's the warning:
Warning: strpos() [function.strpos]: Empty delimiter in /home/site/public_html/website/form/post.php on line 15

MattAU

3:34 am on Nov 8, 2008 (gmt 0)

10+ Year Member



When $blockwords gets exploded, the last variable in the $useBlocks array is an empty string. This means that strpos('somevalue','') is getting called, causing the error.

Something like the following will fix it:

foreach($useBlocks as $blockWord)
{
if($blockword)
{
// Rest of code is as before
}
}

By the way... It's much easier to read code (especially other people's code) when it's well spaced and not all on one line... :)

joe da laidoff plumr

7:09 am on Nov 8, 2008 (gmt 0)

10+ Year Member



I fixed the error just before your reply... but I have no Idea how I fixed it... I was basically playing with the code... removing white spaces, commas, and what not..trial by error... after about 2 hours of playing around, the error went away... but I dont know what I did to solve the problem... I will try out what you have suggested on my next site, dont want to experiment with the code any more now that it's working... wonder if that comma behind badword3 had anything to do with the error because looking over the code that is the only difference i can see after correcting
----------------------------------------------------------
before:
$blockwords="badword 1, badword 2, badword 3,";

after:
$blockwords="badword 1, badword 2, badword 3";

...that is the only difference that I can see between my correction, but I do not have a trained eye.
anyways the problem is fixed but I will copy and paste your suggestion to my hard drive for future reference

MattAU

7:57 am on Nov 8, 2008 (gmt 0)

10+ Year Member



Yes, the comma was the problem. The solution I gave was to ensure it doesn't happen whether there's an extra comma or not.

joe da laidoff plumr

8:51 am on Nov 8, 2008 (gmt 0)

10+ Year Member



thanks for the reply/help... Made a note of your suggestion along side the script... for the future use of the script. Thanks