Solved

Rewite existing code

Posted on 2006-07-12
9
239 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

634 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