Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Rewite existing code

Posted on 2006-07-12
9
Medium Priority
?
242 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 1000 total points
ID: 17094948
This looks good to me - what is your problem with it?
0
 
LVL 29

Assisted Solution

by:TeRReF
TeRReF earned 1000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

916 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