Jump to content

[SOLVED] PHP Search Text Function


ipwnzphp

Recommended Posts

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";
}

Link to comment
Share on other sites

<?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}";
?>

Link to comment
Share on other sites

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";

?>

Link to comment
Share on other sites

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
}

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.