How to check specific words in a textarea?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marco Langes

    How to check specific words in a textarea?

    Hello,
    first of all: Excuse me for some bad English you might experience during the reading of this.

    So I made this form with an textarea, people can write a comment in here but I wanted to create something that my function counts specific words in it and says:
    Word 1: 12 times
    Word 2: 3 times
    etc
    etc

    Code of what I have so far (I'm still a beginner in PHP):
    Code:
    $commentaar = trim($_POST['commentaar']);
    
    function commenteva($commentaar)
    	{
    		global $commentaar;
    		$commentaar = nl2br($commentaar); //newlines	
    		
    		$positive = array("goed", "top", "beste"); //Positive words
    		$negative = array("slecht", "kut", "beter", "zuigt", "haat"); //Negative words
    		
    		$plengte = strlen($positief);
    		$nlengte = strlen($negatief);
    		
    		echo ("<br />Aantal positieve woorden: " . $plengte);
    		echo ("<br />Aantal negatieve woorden: " . $nlengte);
    		
    			if($plengte > $nlengte)
    			{
    				echo("<br />Commentaar is positief");
    			}
    			elseif($nlengte > $plengte)
    			{
    				echo("<br />commentaar is negatief");
    			}
    			else
    			{
    			echo("<br />commentaar is neutraal");
    			}
    	}
    I just can't get it to work, each try I get different errors, I hope I am clear enough.

    Thanks
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    Have a look at this function.
    PHP preg_match_all( )

    It checks for a pattern inside a string and returns the number of matches.

    Comment

    Working...