I use the strpos function to see if I should send an email to the user or not. If the array POST contains a specific word in a form field and another string does not contain it, I should send an email, otherwise I should move on and do nothing:
$my_string = 'word';
$email = 'john@doe.com';
if ((strpos($_POST['field'],'word') !== FALSE || strpos($_POST['field'],'WORD') !== FALSE) &&
(strpos($my_string,'word') === FALSE || strpos($my_string,'WORD') === FALSE)) {
$subject = 'Email subject';
$message = 'Email message';
mail($email,$subject,$message);
}
In this particular case, $my_string contains ‘word’, therefore the email should not be sent, but it’s sent anyways. What am I doing wrong?
Doesn’t answer your question, but you might want to check out http://www.php.net/manual/en/function.stripos.php
Cups
October 24, 2011, 2:52pm
3
stri pos is case insensitive
$_POST['field'] =" string with a WORD in it";
if(stripos($_POST['field'],'word')){
echo 'send it';
}
else{
echo 'dont send it';
}
This works because stripos() returns the position of the match, which when tested in a boolean fashion returns true.
if() works by applying a test to see if something equates to true.
true is true so echo ‘send it’;
If not found then stripos returns false, false it not true so the if() failure evokes the else {}
I didn’t know that there was a function named stripos, I thought that there was only strpos
Just one thing: writing only
if(stripos($_POST['field'],'word'))
didn’t work, I had to write
if(stripos($_POST['field'],'word') !== false)
to make it work. Just in case somebody needs to do the same thing