SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Is there a Php function to search for a list of words in a given text block?

    hi,

    What is the Php function to search for a list of words in a text and if one of the words matches then return true.

    So say we want to search for these words:
    $words = array('wag', 'dog', 'tail');

    for ($i=0, $i<count(documents), ++$i) {

    $documnt = documents[$i];

    if (find_words($words, $document)) {

    $match[$i] = 'yes';

    }

    }


    so is there a Php function that would do what find_words() is doing
    in above sample code?

    Regards,

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I did what you suggested, but I am getting Error messages.

    That is I have:

    $fraud_words = array('dog', 'wag', 'tail');

    And

    for ($i = 0; $i < count($fraud_words); ++$i) {

    $word = $fraud_words[$i];

    if (preg_match($word, $headline)) {

    $fraud = 'yes';
    }
    }


    And I get this Error message:

    Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in /var/www/html/dreamdates.com/admin_dd.php on line 368


    ThanX,

    Quote Originally Posted by logic_earth View Post

    Anoox search engine volunteer

    www.anoox.com

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Might work if you bothered to learn how to use preg_match.
    I'm not going to hand you the answer on a silver platter.

    You've been here long enough, for once try and solve your problems by learning.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why are you bothering answering my question if your attitude is to answer a follow up question like this:
    "I'm not going to hand you the answer on a silver platter."

    PLEASE do not answer any more questions of mine so that my time is not wasted reading
    such crap from you.

    I mean I gladly answer peoples question and provide all sort of volunteer work without insulting
    whom I am helping. So please if you cannot do the same then do not bother to provide peace
    meal help followed by an insult such as: "for once try and solve your problems by learning"

    Thank you.

    Quote Originally Posted by logic_earth View Post
    Might work if you bothered to learn how to use preg_match.
    I'm not going to hand you the answer on a silver platter.

    You've been here long enough, for once try and solve your problems by learning.

    Anoox search engine volunteer

    www.anoox.com

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Have fun trying to solve it on your own then.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no built-in function for this. Try the following code

    PHP Code:
    $words = array('wag''dog''tail');
    $wordsPattern '#(' implode("|"$words) . ')#i';
    foreach(
    $documents as $doc)
    {
            
    $found preg_match($wordsPattern$doc);


  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gvre View Post
    There is no built-in function for this. Try the following code
    Bah, wasn't suppose to give away the answer! D
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    gvre , ThanX I will try that.

    Anoox search engine volunteer

    www.anoox.com

  10. #10
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    gvre, I tried that with the method, but it is not working.
    Can you suggest what to change to this code to get it to work OK?
    ThanX

    for ($i = 0; $i < count($fraud_words); ++$i) {

    $word = $fraud_words[$i];
    $word = '/' . $word . '/i';

    if (preg_match($word, $headline)) {

    $fraud = 'yes';

    } else {

    $fraud = 'no';

    }

    }//CLOES For Loop

    Quote Originally Posted by gvre View Post
    There is no built-in function for this. Try the following code

    PHP Code:
    $words = array('wag''dog''tail');
    $wordsPattern '#(' implode("|"$words) . ')#i';
    foreach(
    $documents as $doc)
    {
            
    $found preg_match($wordsPattern$doc);


    Anoox search engine volunteer

    www.anoox.com

  11. #11
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Oh boy, I just knew you would come back! Even when the answer is given to you on a silver platter...Seriously, one of these days you just need to stop and actually learn the fundamentals of PHP or any programming language before you come here begging for help with every tiny problem you run into. You may think I'm being cruel and mean with my attitude, well I am. No one learns anything by getting all the answer in life handed to them, why should programming be the same?

    If you had any grasp of the fundamentals of PHP, or programming in general you could easily see the error you made. But you cannot because all you have ever done is ask for an answer and took it without question or understanding it. Do you want to know why your code does not work? Look real hard at it, imagine the process it goes though in your head. What is the result?

    Furthermore, get ride of the for loop, do not use a foreach loop or any loop for that matter, its not required. gvre, gave you every single thing you needed, minus the foreach loop. How could you miss that?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  12. #12
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    To say that you are a rude obnoxious jerk is putting it mildly.
    After all, 1st I asked you not to answer to my messages any further since I have no need of your attitude.

    2nd, you have NO idea of what work load I have, given that I am literally doing 10 peoples work, at least.

    3rd, and this is my last message to you EVER, for this new site that I am creating I have written about 8,000 lines
    of Php code and have asked a total of 6 question on Sitepoint about it, one of which is/was this question.

    Good day.



    Quote Originally Posted by logic_earth View Post
    Oh boy, I just knew you would come back! Even when the answer is given to you on a silver platter...Seriously, one of these days you just need to stop and actually learn the fundamentals of PHP or any programming language before you come here begging for help with every tiny problem you run into. You may think I'm being cruel and mean with my attitude, well I am. No one learns anything by getting all the answer in life handed to them, why should programming be the same?

    If you had any grasp of the fundamentals of PHP, or programming in general you could easily see the error you made. But you cannot because all you have ever done is ask for an answer and took it without question or understanding it. Do you want to know why your code does not work? Look real hard at it, imagine the process it goes though in your head. What is the result?

    Furthermore, get ride of the for loop, do not use a foreach loop or any loop for that matter, its not required. gvre, gave you every single thing you needed, minus the foreach loop. How could you miss that?

    Anoox search engine volunteer

    www.anoox.com

  13. #13
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    How about a loop using stripos?

    And please, let's keep it friendly.

  14. #14
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Immerse View Post
    And please, let's keep it friendly.
    Trying, but it is hard when they don't bother to learn, instead want everything handed to them. Even when it is...bah.

    I mean how hard is:
    PHP Code:
    $words join( array( 'word1''word2' ), '|' );
    if ( 
    preg_match"/$words/i"$soruce ) )
       
    // found whatever in source 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  15. #15
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No stripos will not do the job.
    Since I need to look for multiple words within a document.
    But ThanX anyway.

    Actually I think I have finally come up with a solution that does th ejob.

    Anoox search engine volunteer

    www.anoox.com

  16. #16
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WorldNews View Post
    Actually I think I have finally come up with a solution that does th ejob.
    Congratulations on copy and pasting!
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  17. #17
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    logic_earth, you need to get a life rather hanging around here like a troll and
    insulting people rather than being of help.
    I mean how many times do I have to ask you not to comment to my posts and
    you still like a troll send 1 message after another! Really amazing


    Quote Originally Posted by logic_earth View Post
    Congratulations on copy and pasting!

    Anoox search engine volunteer

    www.anoox.com

  18. #18
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    At the risk of getting smacked for backseat moderation - knock it off, both of you. If you can't stand each other's posts do what I do with the people here I can't stand and use the ignore poster feature, aka the "killfile"

  19. #19
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WorldNews View Post
    No stripos will not do the job.
    Since I need to look for multiple words within a document.
    Why not take each word at a time and see if it exists in the document with strpos()?

    PHP Code:
    function find_words($words$document)
    {
        foreach (
    $words as $word) {
            if (
    strpos($words$word) !== FALSE) {
                return 
    TRUE;
            }
        }
        return 
    FALSE;

    Salathe
    Software Developer and PHP Manual Author.

  20. #20
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Why not take each word at a time and see if it exists in the document with strpos()?

    Code:
    function find_words($words, $document)
    {
        foreach ($words as $word) {
            if (strpos($words$document, $word) !== FALSE) {
                return TRUE;
            }
        }
        return FALSE;
    }
    *squiggles out a word* Ah fixed it.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  21. #21
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Where is the Ignore button?
    I was actually looking for exactly that to use it against this rude logic_earth person.

    Anoox search engine volunteer

    www.anoox.com

  22. #22
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Thanks StarLion. We can't be giving WorldNews code that he can copy and paste and works! It seemed futile to be handing out code that he's got no idea what to do with nor will take the time to learn; it's a dirty tactic but one I've used before to get someone to think about the code they're being offered and to start to learn how to debug trivial every-day problems.
    Salathe
    Software Developer and PHP Manual Author.


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
  •