I have wasted two days trying to find a solution to my problem and thought I could use some help from you PHP gurus.

I want to search for a string occurence within another string and know this can be easily done with eregi() but what I want is more problematic.
Here's what I did:

$lookingforthis="STRING-836161F5";
$inthis="STRING";
if(!eregi($lookingforthis, $inthis)) { echo "Not found";}

This doesn't work for me since eregi() is unable to find "STRING" in "STRING-836161F5". I was wondering if there's some other specialized function for what I want.

Now some background: What I do is I get a user's computer name in the $lookingforthis format and want to compare it against a list of usernames; The computer name always contains a dash so eregi() won't work for me 🙁

Your help is highly appreciated!!!

    Your example would work if you reversed the eregi() arguments (since the shorter string can be in the longer string, but not vice versa). However, if you are using PHP 5, a simpler and more efficient solution would be to use [man]stripos/man:

    $needle = 'STRING';
    $haystack = 'STRING-123456';
    If(stripos($haystack, $needle) === false)  // be sure to use "===", not "=="
    {
       echo "Not found";
    }
    

      Alternatively, if you aren't yet using PHP 5 (even though you really should be, by now :p), you could eliminate the case issue and use [man]strpos/man:

      $needle = 'STRING';
      $haystack = 'STRING-123456';
      if(strpos(strtolower($haystack), strtolower($needle)) === FALSE) {
          // not found
      }

      Or, you could of course go for a regexp search (though I'd suggest using the preg_() functions rather than the ereg() functions as the latter have long since been deprecated):

      if(!preg_match('/' . preg_quote($needle, '/') . '/i', $haystack)) {
          // not found
      }

        Thank you for your suggestions!
        The examples worked but I realized they're not appropriate for what I want.

        If $needle is one letter short it will still be found in $haystack and that's not acceptable for me (i.e.: the function returns true even if $needle is "ST" instead of "STRING").

        I am wondering if there's a function that would accept boundaries or something that would work for that I want.

        BTW, I am using PHP5 :o

          If you want to match the name where it begins at the start of the string and is followed by a hyphen:

          if(stripos($haystack, $needle . '-') !== 0) // 0 == start of string
          {
             // not found
          }
          

          Of course, if someone entered "S-" this would still be a problem, unless you filtered out such data or rejected it first.

            Thanks NogDog, it seems that I've found the boundary solution with preg_match.
            What I did was:

            if(!preg_match("/\b$needle\b/i", $haystack))  { echo "Not Found";} else echo "Found";

            This sets the boundary to $needle and is exactly what I was looking for 😃

            Thank you all for replying!!!😉

              Write a Reply...