Turning 4 duplicate functions into 1

I have 4 functions for searching through an XML feed. Each search function is on its own separate php page. Each page is then called as an include in the main page to run its particular search function. These functions only difference is the variable that is passed to them to search for. E.g.

$searchone = ‘cheese’;
$searchtwo = ‘ham’;
$searchthree = ‘egg’;
$searchfour = ‘bacon’;

What I would like to do is have one function that runs the function for each word out putting the results each time — using a loop I guess. I think I need to start by storing the search terms as an array but from here Im struggling.

Would someone be able to shine some light on this?

without seeing the actual search function the barebones would be something like:


function searchfeed($feed, $str) {
    
    if(stristr($feed, $str)) {
        // do something
    }
}


usage:


$searchone = searchfeed($incomingxml, 'cheese');
$searchtwo = searchfeed($incomingxml, 'ham');


If you can show us the actual function then it would be easier to give you an accurate answer.

That’s great. I have a background in flash actionscript and thought you might be able to pass values to functions like that.

I thought the search function would be to large to post here. It uses simplexml_load_string to search though an XML feed for ‘posts’ with a certain term. When it finds one it then write it in html form to a new file.

Would this sort of thing work / be effective?

function mysearchfuction() {
 
    $num = func_num_args(); // 
	//echo $num;
 
    $total = 0;
    for( $i = 0; $i < $num; ++$i ) {
        echo "<br>do search using ";
		echo func_get_arg($i);
                      /// do search code here
    }
 
}
 
$result = mysearchfuction('ham', 'eggs', 'cheese');  // call function and pass terms

How would you determine which keyword matched the search document? Or does this not matter?


<?php
error_reporting(-1);
ini_set('display_errors', true);

function string_contains($string, array $keywords){
  foreach($keywords as $keyword){
    if(false !== stripos((string)$string, $keyword)){
      return true;
    }
  }
  return false;
}

if(string_contains('For breakfast, I like bacon and sausages.', array('beans', 'bacon'))){
  #found
}
?>

Not 100% sure I understand you.

I am thinking aloud here but if you imagine I have a source XML feed. From that feed I want to search for certain keywords. When I find a match I want to write this to a new XML file. Effectively ending up with a XML file for each keyword.

I would like to repeat this process via a cron so that when the source XML feed updates the new items are added to my created XML files - like creating an archive.

Am I approaching this the right way?

I think I follow you.

In which case, you’re going to need to know which keyword was found then, so you can name your ‘archive’ XML file. Yes?

The sample function I provided isn’t going to be of much use then.

What about…


<?php
error_reporting(-1);
ini_set('display_errors', true);

$keywords = array(
  'bacon',
  'beans',
  'sausage',
  'eggs'
);

function string_contains($string, $keyword){
  return false !== stripos((string)$string, $keyword);
}

foreach($keywords as $keyword){
  if(string_contains($xml->feed->item, $keyword)){
    #create $keyword.archive.xml
  }
}
?>

Thanks so much! That makes sense.

Now I just have to work out my search and XML write functions!