...

View Full Version : For Each Loop Help



Pennimus
08-13-2007, 11:32 PM
The below loop only seems to increment $abusecount for the first value in the array - if $q contains any of the other array values the script executes. What have I done wrong?



$stopwords = array('<', '>', 'bcc:', 'cc:', 'a href', '[url]');
$abusecount = 0;
foreach ($stopwords as $check){
if (strpos($q, $check) > 0){
$abusecount++;
}
}

if ($abusecount > 0){
die();
} else {
// Execute
}

Fumigator
08-13-2007, 11:45 PM
The strpos() function returns the position of the string, if it is found, and "false", if it is not. If the string is found in the first byte, i.e. the "0"th byte, strpos() returns "0". Therefore, you cannot do if (strpos($q, $check) > 0), you must instead do if (strpos($q, $check) === false) {/*do false logic */} else {$abusecount++;}

This is covered in depth in the PHP manual (http://us2.php.net/manual/en/function.strpos.php).

Pennimus
08-13-2007, 11:57 PM
Ah yes, thanks Fumigator.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum