Solved

Rewite existing code

Posted on 2006-07-12
9
236 Views
Last Modified: 2008-03-10
Here is a script that was made by someone on EE, and I was wondering if there is a different or better way to write the function in this script.

This will search the textbox for a certain word and only return all other lines with that word in it.
Example:
red bull energy
juice drink
red bull hot cakes
fruit baskets
peanut butter and red bull
red bull energy drink
fine wine
berry jelly

This should be returned:
red bull energy
red bull hot cakes
peanut butter and red bull
red bull energy drink



<?php
if (isset($_POST['Submit']))
{

     function phraseSearch($inThesePhrases, $searchForThis)
     {
          foreach ($inThesePhrases as $s)
          {
               if (strpos(strtolower($s), strtolower($searchForThis)) !== false)
               $farray[] = $s;
          }
          return $farray;
     }
     $array = explode("\n",$_POST['field1']);
     $results = phraseSearch($array, $_POST['field2']);
     var_dump($results);
     
}
else
{

     // UNSET ALL GLOBAL VARS FIRST
     function unset_all_vars($a)
     {
          foreach($a as $key => $val)
          {
               unset($GLOBALS[$key]);
          }
          return serialize($a);
     }

     unset_all_vars(get_defined_vars());
}
?>
<form id="form1" name="form1" method="post" action="testing.php">
     <label><textarea name="field1" cols="30" rows="10" id="field1"></textarea></label>
     <p>Filter out all words except words with this phrase in it:</p>
     <p><label><input name="field2" type="text" id="field2" /></label></p>
     <p><?php foreach ($results as $val) echo $val; ?></p>
     <p><label><input type="submit" name="Submit" value="Submit" /></label></p>
</form>
0
Comment
Question by:ray-solomon
  • 6
  • 2
9 Comments
 
LVL 35

Accepted Solution

by:
Raynard7 earned 250 total points
ID: 17094948
This looks good to me - what is your problem with it?
0
 
LVL 29

Assisted Solution

by:TeRReF
TeRReF earned 250 total points
ID: 17094960
What do you want changed?
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17095102
I implemented this into another script and the function gets called inside of a conditional statement like this:

$r2 is the word to search for.
$field1 is the textarea box the words come from.

When i dump $x2_array, it echo's an array. Good
When i dump $r2, it echo's the word. Good
but when I dump $x2_results, it echo's NULL
not good

so the problem could be the function?

      elseif ($r2 !== "" && $field1 !== "" && $checkbox == "0")
      {
            $x2_array = explode("\n", $field1);
            $x2_results = phraseSearch($x2_array, $r2);
            $x2_data = implode("\n", $x2_results);
            $x2_trimmed = strtolower(stripslashes(preg_replace("/\s\s+/", "\n", $x2_data)));
            $x2 = $x2_trimmed;
            $x2_quotes = addQuotes($x2);
            $x2_brackets = addBrackets($x2);
            $textarea5 = $x2;
            $textarea6 = $x2_brackets;
            $textarea7 = $x2_quotes;
            $textarea8 = $x2."\r\n".$x2_quotes."\r\n".$x2_brackets;
            var_dump($x2_results);
      }
      elseif ($r2 !== "" && $field1 !== "" && $checkbox == "")
      {
            $x2_trimmed = cleanText($field1);
            $x2 = phraseReplace($replacethis, $r2, $x2_trimmed);
            $x2_quotes = addQuotes($x2);
            $x2_brackets = addBrackets($x2);
            $textarea5 = $x2;
            $textarea6 = $x2_brackets;
            $textarea7 = $x2_quotes;
            $textarea8 = $x2."\r\n".$x2_quotes."\r\n".$x2_brackets;
      }
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 29

Expert Comment

by:TeRReF
ID: 17095150
Try changing this line:
             if (strpos(strtolower($s), strtolower($searchForThis)) !== false)
into this one:
             if (strpos(strtolower(trim($s)), strtolower(trim($searchForThis))) !== false)
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17095176
It is still retuning NULL
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17095571
Okay I made a small mockup script that closely relates to what is going and hopefully shed some light on this issue:
What could the problem be? I get the same error message here as I do in my real script:
Warning: implode(): Bad arguments. in /blah on line 20

<?php
if (isset($_POST['Submit']))
{

     function phraseSearch($inThesePhrases, $searchForThis)
     {
          foreach ($inThesePhrases as $s)
          {
               if (strpos(strtolower($s), strtolower($searchForThis)) !== false)
               $farray[] = $s;
          }
          return $farray;
     }

      
if ($r2 !== "" && $field1 !== "" && $checkbox == "0")
     {
          $x2_array = explode("\n", $field1);
          $x2_results = phraseSearch($x2_array, $r2);
          $x2_data = implode("\n", $x2_results);
          $x2_trimmed = strtolower(stripslashes(preg_replace("/\s\s+/", "\n", $x2_data)));
          var_dump($x2_array);
              var_dump($x2_results);
              var_dump($x2_data);
              var_dump($x2_trimmed);
     }
     
}
else
{

     // UNSET ALL GLOBAL VARS FIRST
     function unset_all_vars($a)
     {
          foreach($a as $key => $val)
          {
               unset($GLOBALS[$key]);
          }
          return serialize($a);
     }

     unset_all_vars(get_defined_vars());
}

      
?>
<form id="form1" name="form1" method="post" action="">
  <label>
  <textarea name="fielda" cols="30" rows="10" id="fielda">red bull energy
juice drink
red bull hot cakes
fruit baskets
peanut butter and red bull
red bull energy drink
fine wine
berry jelly</textarea>
  <br />
  <br />
  <input name="r2" type="text" id="r2" value="red bull" />
  <br />
  <br />
  <input name="checkbox" type="checkbox" value="0" checked="checked" />
  <br />
  <br />
  <input type="submit" name="Submit" value="Submit" />
  </label>
</form>
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17095583
wait forget that last post, there is a typo for $field1 textbox name
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17095611
This demo appears to work correctly, not what I was hoping to see, so that means there is something else wrong in my real script.

I will to look it over again..
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17097402
My problem was with a different part of the script. I just split points up for taking the time to look at my code. Thanks anyway.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question