php - Foreach strpos problem. Find string in another string -
i'm trying script work.
the idea if input string ($query)
doesn't start '/t'
, contains 1 of $trigger
words, $error
set.
i can't work , 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 $trigger) { if (strpos($query, $trigger)) { $error = true; } } } } if ($error) { echo "fail"; } else { echo "pass"; } ?>
that should have triggered error doesn't seem be. doing wrong?
if function strpos
fails find string returns false
. note if search string found @ beginning 0
returned.
change
if (strpos($query, $trigger)) {
to
if (strpos($query, $trigger) !== false) {
Comments
Post a Comment