Solved

Rewite existing code

Posted on 2006-07-12
9
232 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now