SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with strpos function

    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:

    PHP Code:
    $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?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Doesn't answer your question, but you might want to check out http://www.php.net/manual/en/function.stripos.php

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    stripos is case insensitive

    PHP Code:
    $_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 {}

  4. #4
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't know that there was a function named stripos, I thought that there was only strpos

  5. #5
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just one thing: writing only

    PHP Code:
    if(stripos($_POST['field'],'word')) 
    didn't work, I had to write

    PHP Code:
    if(stripos($_POST['field'],'word') !== false
    to make it work. Just in case somebody needs to do the same thing


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •