ipwnzphp Posted July 24, 2008 Share Posted July 24, 2008 I need a function in php where i can search the text then do if statements based off the keywords. The code below states that if it finds joomla then its correct. but lets say i need to find web design also. if both are there it needs to say we fount both if only 1 is there it needs to say we fount only 1 $search_for = "joomla"; $the_items_description = "i need some joomla and web design work done"; function textsearch($the_items_description) { global $search_for; $pos = strpos($the_items_description, $search_for); if ($pos === false) { return false; } else { return true; } } if (textsearch($the_items_description) == true) { echo "correct"; } else { echo "incorrect"; } Quote Link to comment Share on other sites More sharing options...
teynon Posted July 24, 2008 Share Posted July 24, 2008 <?php $searchWords=array("joomla", "web design"); $string="i need some joomla and web design work done"; $wordResults=0; $totalWords=count($searchWords); function searchForWord($string, $search) { $search="/{$search}/i"; if (preg_match($search, $string)) { return 1; } return 0; } foreach ($searchWords as $search) { $wordResults+=searchForWord($string, $search); } echo "Found {$wordResults} of {$totalWords}"; ?> Quote Link to comment Share on other sites More sharing options...
ipwnzphp Posted July 24, 2008 Author Share Posted July 24, 2008 Thanks works prefect! Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 24, 2008 Share Posted July 24, 2008 That's a little much. Also, preg_match is not very efficient for this: <?php function textsearch($desriptionStr, $searchAry) { $matchCount = 0; foreach ($searchAry as $searchStr) { $matchCount += (strpos($desriptionStr, $searchStr)!=false); } return $matchCount; } $search_for = array('joomla', 'web design'); $the_items_description = "i need some joomla and web design work done"; echo "There are " . textsearch($the_items_description, $search_for) . " matches"; ?> Quote Link to comment Share on other sites More sharing options...
ipwnzphp Posted July 25, 2008 Author Share Posted July 25, 2008 Next question what if i wanted to add if statements to it like if ($searchStr == "joomla") { $site = f_open("http://www.google.com"); } Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 25, 2008 Share Posted July 25, 2008 That depends. Without knowing how this is to be implemented I don't know how best to build a solution. It could be included in the function or not. here is one solution: <?php function textsearch($desriptionStr, $searchAry) { $matchAry = array(); foreach ($searchAry as $searchStr) { $matchAry[] = $searchStr; } return $matchCount; } $search_strings = array('joomla', 'web design'); $description = "i need some joomla and web design work done"; $matches = textsearch($description, $search_strings); echo "There are " . count($matches) . " matches"; if (in_array('joomla', $matches)) { $site = f_open("http://www.google.com"); } if (in_array('web design', $matches)) { //Do something else } ?> Quote Link to comment Share on other sites More sharing options...
ipwnzphp Posted July 25, 2008 Author Share Posted July 25, 2008 There are 0 matches Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/gurumani/public_html/quotesl/system/index.php on line 106 Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/gurumani/public_html/quotesl/system/index.php on line 110 Quote Link to comment Share on other sites More sharing options...
ipwnzphp Posted July 25, 2008 Author Share Posted July 25, 2008 the code below is showing fount asp even though asp is not in the $description var. How do i fix this? function textsearch($desriptionStr, $searchAry) { $matchAry = array(); foreach ($searchAry as $searchStr) { $matchAry[] = $searchStr; } return $matchAry; } $search_strings = array('joomla', 'web design', 'asp'); $description = "i need some joomla and web design work done"; $matches = textsearch($description, $search_strings); echo "There are " . count($matches) . " matches"; if (in_array('joomla', $matches)) { echo "<br><br>Fount Joomla<br><br>"; } if (in_array('web design', $matches)) { echo "Fount Web Design<br><br>"; } if (in_array('asp', $matches)) { echo "Fount ASP"; } Quote Link to comment Share on other sites More sharing options...
teynon Posted July 25, 2008 Share Posted July 25, 2008 I beg to differ the fault of using preg_match. Using preg_match will allow for wild cards and advanced searching. Quote Link to comment Share on other sites More sharing options...
ipwnzphp Posted July 25, 2008 Author Share Posted July 25, 2008 I got it fixed now i think and here was what i came up with to use. <? $Desc = "$_POST[desc]"; // Joomla function find_joomla($Desc) { $keyword = "joomla"; $find = strpos($Desc, $keyword); if ($find == true) { echo $keyword . "Found"; } } echo find_joomla($Desc); // ASP function find_asp($Desc) { $keyword = "asp"; $find = strstr($Desc, $keyword); if ($find == true) { echo $keyword . "Found"; } } echo find_asp($Desc); ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 25, 2008 Share Posted July 25, 2008 I beg to differ the fault of using preg_match. Using preg_match will allow for wild cards and advanced searching. If the OP stated he needed to search using wilecards or regular expressions then, by all means, preg_match() woud be the way to go. But his request didn't mention any such thing and the manual for preg_match() clearly states: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.