#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Finding Words in String?


    I've tried preg_match and preg_greg but can't find any way to get this to work.

    I have a string like below and I'm trying to find 2 or more words in the string and if it is found just return true.

    $stringtoparse = 'a portable iphone covert camera is a mobile camera';

    So if the I find any sequence of words in $stringtoparse I want to have a function return true.

    I want to find phrases like "portable iphone" or "portable covert" or "iphone camera" or "portable iphone covert". The phrase words may or may not be in sequence.

    Is there a way to do this in php?
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,760
    Rep Power
    9397
    By "not in sequence" you mean that the component words can appear anywhere in the string? Or that they have to appear together but in any order?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Well, what I meant is this. I'm trying to "mimic" a database search but using php because of a contraint I have.

    Given a search phrase like: "big red tomatoes"

    And say given these 4 string to search:

    "Big tomatoes are great to pick and eat."

    "Big red tomatoes are great to pick and eat."

    "Tomatoes are great to pick and eat."

    "Red tomatoes are great to pick and eat."

    "Apples are great to pick and eat."

    I'm looking for an easy way to see if the search string is in the string to search. In the above example the first 4 phrases would match. The last would not. Is there a php function that can do this.


    Originally Posted by requinix
    By "not in sequence" you mean that the component words can appear anywhere in the string? Or that they have to appear together but in any order?
  6. #4
  7. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,760
    Rep Power
    9397
    So any string that contains at least one of the words.

    Split the phrases into individual words like
    PHP Code:
    $words = array("portable""iphone""portable""covert""iphone""camera""portable""iphone""covert"); 
    Grab just the unique ones to make life easier, then search the string for each of the words in turn.

    Or you can make it a regex with
    Code:
    /portable|iphone|convert|camera/
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Thanks,

    Is there a way to find at least 2 of the words is the search text if 2 of more search words are entered?

    Originally Posted by requinix
    So any string that contains at least one of the words.

    Split the phrases into individual words like
    PHP Code:
    $words = array("portable""iphone""portable""covert""iphone""camera""portable""iphone""covert"); 
    Grab just the unique ones to make life easier, then search the string for each of the words in turn.

    Or you can make it a regex with
    Code:
    /portable|iphone|convert|camera/
  10. #6
  11. Banned
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,938
    Rep Power
    0
    Hi,

    not sure what kind of solution you expect, but what's wrong with a simple loop?

    PHP Code:
    <?php

    function search_with_phrase($input$phrase) {
        
    $words explode(' '$phrase);
        if (!
    $words)
            return 
    false;
        
    $minimum_matches count($words) <= 2;
        
    $matches 0;
        foreach (
    $words as $word) {
            if (
    strpos($input$word) !== false) {
                
    $matches++;
                if (
    $matches == $minimum_matches)
                    break;
            }
        }
        return 
    $matches >= $minimum_matches;
    }
        
    $input 'The quick brown fox jumps over the lazy dog';
    $phrase 'quick brown';

    var_dumpsearch_with_phrase($input$phrase) );
  12. #7
  13. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,760
    Rep Power
    9397
    Originally Posted by glennfr
    Is there a way to find at least 2 of the words is the search text if 2 of more search words are entered?
    Loop method (as Jacques posted): count the number of times you find a word, make sure it's at least 2.
    Regex method: preg_match_all(), array_unique() on the [0] array of matched words, count() that it's at least 2.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Thanks much Jacques,

    The function search_with_phrase($input, $phrase) works really good. How could I make the $phrase case insensitive?

    Originally Posted by Jacques1
    Hi,

    not sure what kind of solution you expect, but what's wrong with a simple loop?

    PHP Code:
    <?php

    function search_with_phrase($input$phrase) {
        
    $words explode(' '$phrase);
        if (!
    $words)
            return 
    false;
        
    $minimum_matches count($words) <= 2;
        
    $matches 0;
        foreach (
    $words as $word) {
            if (
    strpos($input$word) !== false) {
                
    $matches++;
                if (
    $matches == $minimum_matches)
                    break;
            }
        }
        return 
    $matches >= $minimum_matches;
    }
        
    $input 'The quick brown fox jumps over the lazy dog';
    $phrase 'quick brown';

    var_dumpsearch_with_phrase($input$phrase) );
  16. #9
  17. Banned
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,938
    Rep Power
    0
    Originally Posted by glennfr
    How could I make the $phrase case insensitive?
    With stripos().
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    781
    Rep Power
    495
    I am not using PHP (not that I have anything against it, I learnt the basics of it a few years back and found it interesting, but it turned out not to be the best tool for the type of work I am diong), but I would think that with the loop method proposed by Jacques, you'll have to use some PHP finction to make everything lowercase before you start to compare; with a regex method, the regex /i qualifier will probably suffice. (Sorry if my answer is too Perlish and not really PHPist, but the /i qualifier is very general, working with sed, grep, ed, vi, awk, Perl, etc. I don't know how the "i" qualifier is specified in the PHP or PCRE or whatever regex module you are using).

IMN logo majestic logo threadwatch logo seochat tools logo