Solved

Rewite existing code

Posted on 2006-07-12
9
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 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