I’m trying to get this script to work.
The idea is that if the input string ($query) doesn’t start with '/t' AND contains one of the $trigger words, an $error is set.
I can’t get this to work and I’m not sure why.
<?php
$error = false;
$triggers = array('sell', 'buy', 'trade', 'trading');
$query = 'buying stuff';
if (!empty($query)) {
if (substr($query, 0, 2) != '/t') {
foreach ($triggers as $trigger) {
if (strpos($query, $trigger)) {
$error = true;
}
}
}
}
if ($error) {
echo "fail";
}
else {
echo "pass";
}
?>
That should have triggered the error but it doesn’t seem to be. What am I doing wrong?
If the function
strposfails to find the string it returnsfalse. Also note that if the search string is found at the very beginning a0is returned.Change
to