Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    strpos with multiple words

    Hi I am trying to check for either of 2 words, in a string
    and then do something.

    Right know this below only works with just one word?
    is there a simple to check for multiple words?

    PHP Code:

    $string 
    "one two three four five";

    $find "two";
    //$find2 = "three";

    $status strpos($string$find);

    if (
    $status == true) echo "Found";
    else echo 
    "Not Found"
    Thanks
    Sonny
    Last edited by sonny; 12-31-2012 at 09:33 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    regex.
    Also, using if ($status == true) is incorrect. You should be using if ($status !== false) instead, consider searching for one:
    PHP Code:
    $string "one two three four five";

    $find "one";
    //$find2 = "three";

    $status strpos($string$find);

    if (
    $status == true) echo "Found";
    else 
    "Not Found"
    Would produce no results (I presume you intend to actually print the not found, but in this one its just a string sitting by its lonesome ).

  • #3
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    regex.
    Also, using if ($status == true) is incorrect. You should be using if ($status !== false) instead, consider searching for one:
    PHP Code:
    $string "one two three four five";

    $find "one";
    //$find2 = "three";

    $status strpos($string$find);

    if (
    $status == true) echo "Found";
    else 
    "Not Found"
    Would produce no results (I presume you intend to actually print the not found, but in this one its just a string sitting by its lonesome ).
    of course just forgot the echo thats all, as for that if ($status == true) echo "Found";
    thats been working correctly for years, my question was how do I check for another
    word as well


    Have a nice New Year tonight
    Sonny

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Use regex with a preg_match_all instead.

    $status == true is incorrect. If you find position at 0, that is a valid offset but will fail to pass a true check. You need to check for !== false (such as my example with "one" where it finds no results in the above string).

  • #5
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    In case anyone needs to do something like this
    got it, was even simpleier then I first thought.

    PHP Code:
     $find = array('seven''five''ten');
     
    $string "one two three four five";
     
             foreach (
    $find as $n) {
        if (
    strpos($string$n) !== false)
        echo 
    "Found";} 
    Sonny

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Quote Originally Posted by sonny View Post
    In case anyone needs to do something like this
    got it, was even simpleier then I first thought.

    PHP Code:
     $find = array('seven''five''ten');
     
    $string "one two three four five";
     
             foreach (
    $find as $n) {
        if (
    strpos($string$n) !== false)
        echo 
    "Found";} 
    Sonny
    Explode $string into an array and then use http://www.php.net/in_array on it.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    With an array you could easily implode them as well for the pattern:
    PHP Code:
    $find = array('seven''five''ten');
    $string "one two three four five";
    array_map($find'preg_quote');
    $sPattern '/\b(?:' implode('|'$find) . ')\b/';
    preg_match_all($sPattern$string$matchesPREG_OFFSET_CAPTURE); 
    Then iterate the $matches. They'll be in offset 0, and are an array containing the offsets and the string (if you need the offsets for whatever reason). If you don't need the offsets, drop the modifier and simply iterate the $matches[0].

    This would be better since you don't need to worry about adding spaces and matching what you don't want to match. If you were looking for "fifty", it would also match "fiftyfive" while the pattern would not. This way you also don't need to concern yourself with order.
    The pattern matching does take a lot longer though, but the returns of the strpos will degrade as the find counts increase.

    Edit:
    I like Tango's suggestion as well. That will eliminate the issue with finding things like "fifty" and matching "fiftyfive" for example.
    Edit:
    Assuming of course you don't actually care about the offset, in which case I'd still suggest using preg.

    Last edited by Fou-Lu; 12-31-2012 at 11:22 PM.

  • Users who have thanked Fou-Lu for this post:

    tangoforce (01-01-2013)

  • #8
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Hi Guys

    Am I missing something? does this not work?
    it seems to be working for me, or am I getting
    odd results?. put this in a file and test yourself.

    PHP Code:
     $find = array('seven''five''ten');
     
    $string "one two three four five";
     
             foreach (
    $find as $n) {
        if (
    strpos($string$n) !== false)
        echo 
    "Found";} 
    Sonny

  • #9
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Explode $string into an array and then use http://www.php.net/in_array on it.
    Yes I like this way, very simple and clean
    going to test that

    Thanks
    Sonny

  • #10
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Hi this is something like what I need to do, ln_array is most likely not
    the best way to go, but I may be wrong.

    Is this the best way to do that?, I am looking for a page that "either"
    has test or admin in its name, that's basically what I need to do.

    PHP Code:
    $find = array('test''admin');
    $string $_SERVER['REQUEST_URI'];

             foreach (
    $find as $n) {
                     if (
    strpos($string$n) !== false)
        echo 
    "Found";} 
    Sonny

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Whoa, looks like the goal posts have just moved!
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I think its just the understanding that has changed

    These approaches have different purposes.
    Using in_array or an intersection of arrays looks for the same items, not containment items. It is also the fastest of the three since you are comparing individual tokens to other individual tokens.
    Using strpos will find partials. It takes longer than an array check since you need to compare the entire string each time. It cannot find multiples.
    Using preg has the greatest flexibility in what you can do. You can choose to either make partials or exact matches. It is the slowest of the three, but can also find duplicates (the in_array can as well).

    If your goal is to check if a string contains any of the items and can match partials, without caring of which it matches, simply write a function that returns a boolean:
    PHP Code:
    function stringContainsAnyOf($s, array $a)
    {
        
    $result false;
        
    reset($a);
        while (!
    $result && ($cur current($a)))
        {
            
    $result strpos($s$cur) !== false;
            
    next($a);
        }
        return 
    $result;

    That would return true so long as any one of the array items match, but you cannot tell which one it was nor whether it was a partial. Likewise, if you want to use identical, you would use in_array.
    The preg wouldn't require a function to do this since it returns all the matches as an array. No matches = no items in array.
    Last edited by Fou-Lu; 01-01-2013 at 03:37 PM.

  • #13
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Whoa, looks like the goal posts have just moved!
    Please take another look at the first post, nothing has changed
    It's still a string, that needs to be checked for two words "efficiently".
    and with minimal code as always.

    I had this working since the 4th post like shown below
    can you give feedback as to why this is not a good way?
    It's been working perfect so far.

    PHP Code:
     $find = array('seven''five''ten');
     
    $string "one two three four five";
     
             foreach (
    $find as $n) {
        if (
    strpos($string$n) !== false)
        echo 
    "Found";} 
    Hope you had a nice New Year!.
    Sonny

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Quote Originally Posted by sonny View Post
    I had this working since the 4th post like shown below
    can you give feedback as to why this is not a good way?
    It's not that it isn't a good way, there is nothing wrong with what you're doing. All I'm saying is that I would have use in_array() - a php function (that will no doubt be slightly quicker and more efficient) to check if each word is in the array.

    As I said, nothing wrong with what you've done but there are always several ways of doing the same thing
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #15
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    It's not that it isn't a good way, there is nothing wrong with what you're doing. All I'm saying is that I would have use in_array() :
    problem was, in_array only works with exact match, or I would have used it.

    Thanks
    Sonny


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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