I want to find out if a string is within another string. I want to see if the needle is in the haystack, so to speak. I'm nearly certain I should be using the strpos() function. But I want to find a string, with spaces and all, not just a single character or group of chars without spaces.

For example:

$needle = "NO-108 J R RAILEY";
$haystack = "B22-V1 / NO-108 J R RAILEY";

// if needle is found in haystack, return true
if (strpos(stripslashes($needle),stripslashes($haystack)))
   return true;
else
   return false;

This returns false as written. I've clearly written it incorrectly... can anyone clear this up for me? Am I even applying the PHP function correctly?

Thanks,
Mark

    you have needle and haystack wrong way round

     $needle = "NO-108 J R RAILEY"; 
    $haystack = "B22-V1 / NO-108 J R RAILEY"; 
    
    // if needle is found in haystack, return true 
    if  (strpos(stripslashes($haystack),stripslashes($needle))) 
       return true; 
    else 
       return false;
     

      Hah, I'm an idiot. I spent all this time learning about the function on the PHP page, just to overlook this important detail. Thanks for turning me around (in the right direction) πŸ™‚

        actually the [man]strstr[/man] function does exactly what you trying to do.

          Originally posted by devinemke
          actually the [man]strstr[/man] function does exactly what you trying to do.

          Thanks for the reply. I noticed that on the PHP documentation. I thought about using it instead, but then I found this note on the function webpage:

          Note: If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.

          And so that's why I went with strpos() instead-- because I only want to know whether or not it actually occurs.

            For the benefit of others who may run into the same issues as me... let me update the code. If the first position of the needle is 0, the function returns false even though the needle is in the haystack.

            So the updated code:

            if (strpos(stripslashes($haystack),stripslashes($needle)) === false)
               return false;
            else
               return true;
            
            

              Personally, I prefer one-liner return statements. They're not always applicable, but in this case one could be applied.

              Instead of this

              if (strpos(stripslashes($haystack), stripslashes($needle)) === false)
                 return false;
              else
                 return true;

              use this

              return (strpos(stripslashes($haystack), stripslashes($needle)) !== false);

                Hey now I like that! I've never really used one-liners though because I was always taught the long way of writing if statements and returns. But they definitely simplify thingsπŸ™‚

                  Originally posted by Installer
                  Stay with the way you were taught.

                  I think uffis' code is justified here. "If the result of this function is false then return false - if it is true, return true" seems a bit longwinded than just "return the result of this function". The former makes it look like there's more going on than there actually is.

                    return (bool)strpos(stripslashes($haystack),stripslashes($needle)
                    );

                    but theres many ways, its all to do with how readable the code is for others as you may not be the one up keeping it

                      Readable, maintainable, reusable, scalable, extensible, etc.--reasons high-level languages were developed in the first place.

                      And as long as I'm getting myself into trouble; instead of this:

                      if (strpos(stripslashes($haystack), stripslashes($needle)) === false) 
                         return false; 
                      else 
                         return true;

                      use this:

                      $haystack = stripslashes($haystack);
                      $needle   = stripslashes($needle);
                      if (strpos($haystack, $needle) === false) { 
                         return false; 
                      } else  {
                         return true;
                      }

                        Originally posted by sidney
                        return (bool)strpos(stripslashes($haystack),stripslashes($needle)
                        );

                        that actually wont work. well, it does but, iif the needle is found at position 0, you have just returned false, which is definately not the desired behavior.

                        explicity checking for false using either the === or !== operators like said above is the way to go. i know you were just suggesting a way to turn the result into a boolean instead of if else, but i just wanted to point it out because often people dont think of that possibility.

                          whoops coding after comiming in from pub

                          return (bool)strpos(' '.stripslashes($haystack),stripslashes(
                          $needle,1)
                          );

                            This thread has turned into something rather fascinating I think. It's a textbook example of how different people come up with varying ways of accomplishing the same taskπŸ™‚ It's really quite interesting to see the varying approaches. For more complex functions, there are infinite number of methods to do the same thing.

                            But as Albert Einstein once said:
                            "Make it as simple as possible, but no simpler."

                              Originally posted by sidney
                              whoops coding after comiming in from pub

                              return (bool)strpos(' '.stripslashes($haystack),stripslashes(
                              $needle,1)
                              );

                              yeah, but now what happens if all or part of the needle is at position 0? now you cant even find it because your skipping the first character.

                              you MUST use the === or !== operators w/ strpos if your just checking if the needle exists or not. there is no way around that, you just simply need to use it, thats why those operators exist, because without them many things wouldnt be possible.

                              and while were all sharing our insignificantly different methods of using strpos, heres mine πŸ™‚

                              
                              
                              return (false !== strpos($haystack, $needle));
                              
                              

                              also strstr is not really a good way to do it either. you still need to explicty check for false using === or !==

                              take this example

                              
                              $needle = '0';
                              $haystack = '1110';
                              if (strstr($haystack, $needle)) {
                                  echo "$needle is in $haystack";
                              } else {
                                  echo "$needle is NOT in $haystack";
                              }
                              
                              
                              

                              many would expect this to work, but it wont. strstr will return everything from the first occurance of $needle to the end of $haystack

                              so in this case, strstr will return string 0

                              string 0 evaluates to boolean false.

                              hence why you must do this

                              
                              $needle = '0';
                              $haystack = '1110';
                              if (false !== strstr($haystack, $needle)) {
                                  echo "$needle is in $haystack";
                              } else {
                                  echo "$needle is NOT in $haystack";
                              }
                              
                              
                              

                                yeah, but now what happens if all or part of the needle is at position 0? now you cant even find it because your skipping the first character

                                if you look close you wil see i added an extra character to the front of the string

                                  Originally posted by sidney
                                  if you look close you wil see i added an extra character to the front of the string

                                  It's getting kind of messy now, isn't it? Besides; what if the extra character happens to be the first character of the string being searched for, and combined with the string being in just happens to create a match when there otherwise wouldn't have been one? Now you're relying on 0 being treated as equivalent to false.

                                    Originally posted by sidney
                                    if you look close you wil see i added an extra character to the front of the string

                                    It's getting kind of messy now, isn't it?

                                    What if the extra character happens to be the first character of the string being searched for, and combined with the string being in just happens to create a match when there otherwise wouldn't have been one? Now you're relying on 0 being treated as equivalent to false.

                                    And what if there was a match in there after all? Your code would match the accidental one caused by tacking the spurious character on the beginning. That will return 0, be treated as equivalent to false, and you'll get an incorrect result.

                                      Originally posted by Weedpacket
                                      It's getting kind of messy now, isn't it?

                                      What if the extra character happens to be the first character of the string being searched for, and combined with the string being in just happens to create a match when there otherwise wouldn't have been one? Now you're relying on 0 being treated as equivalent to false.

                                      And what if there was a match in there after all? Your code would match the accidental one caused by tacking the spurious character on the beginning. That will return 0, be treated as equivalent to false, and you'll get an incorrect result.

                                      you need to look again, the 1 in the 3 argument means it never looks at the added character and can never return 0

                                      anyway my original point was about readability and
                                      one liners not allways being the best idea