Solved

Replace words in textboxes with conditions

Posted on 2006-07-12
4
231 Views
Last Modified: 2008-03-04
I have an example here that I am unable to figure out. I get stuck when I go to replace a word that is not in the original array. I posted an html version to see if someone could write the php code from scratch for this. I would like to see how it differs from mine and where I went wrong. This contains the instructions of what to do.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test</title>
<style type="text/css">
<!--
.style1 {font-family: Arial, Helvetica, sans-serif; font-size: 10px; }
-->
</style>
</head>

<body>

<form id="form1" name="form1" method="post" action="">
  <label></label>
  <table width="815" height="225" border="1">
    <tr>
      <td colspan="2" rowspan="5"><p class="style3"><span class="style1">Textbox 1
          </span>
        <textarea name="field1" cols="30" rows="10" id="field1">red bull energy
juice drink
red bull hot cakes
fruit baskets
fruit juice
peanut butter and red bull
red bull energy drink
fine wine
berry jelly
red fruit baskets</textarea>
          </p>
        <p>&nbsp;</p>      </td>
      <td width="96" class="style1"><span class="style3">Phrase to replace in each line </span></td>
      <td width="144"><input name="replacethis" type="text" id="replacethis" value="fruit baskets" /></td>
      <td width="405" rowspan="5"><p class="style1">Results 1 = textbox1 </p>
        <p class="style1">Results 2 = If a line within textbox 1 contains the phrase to replace, then replace phrase with Alt 1,  otherwise if the phrase to replace is not in a line within textbox 1, ignore the line. (Do not return the line) </p>
        <p class="style1">Results 3 =  If a line within textbox 1 contains the phrase to replace, then replace phrase with Alt 2,  otherwise if the phrase to replace is not in a line within textbox 1, ignore the line. (Do not return the line) </p>
        <p class="style1">Results 4 =  If a line within textbox 1 contains the phrase to replace, then replace phrase with Alt 3,  otherwise if the phrase to replace is not in a line within textbox 1, ignore the line. (Do not return the line) </p>
        <p>&nbsp;</p>
      <p>&nbsp;</p></td>
    </tr>
    <tr>
      <td class="style1"><span class="style3">Alternative 1 </span></td>
      <td><input name="r2" type="text" id="r2" value="gift baskets" /></td>
    </tr>
    <tr>
      <td class="style1"><span class="style3">Alternative 2 </span></td>
      <td><input name="r3" type="text" id="r3" value="gifts" /></td>
    </tr>
    <tr>
      <td class="style1"><span class="style3">Alternative 3 </span></td>
      <td><input name="r4" type="text" id="r4" value="baskets" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
  <label><br />
  </label>
  <table width="592" border="1">
    <tr>
      <td class="style1">Results 1 </td>
      <td class="style1">Results 2 </td>
      <td class="style1">Results 3 </td>
      <td class="style1">Results 4 </td>
    </tr>
    <tr>
      <td class="style1"><textarea name="t5" cols="30" rows="10" id="t5">red bull energy
juice drink
red bull hot cakes
fruit baskets
fruit juice
peanut butter and red bull
red bull energy drink
fine wine
berry jelly
red fruit baskets</textarea></td>
      <td class="style1"><textarea name="t6" cols="30" rows="10" id="t6">gift baskets
red gift baskets</textarea></td>
      <td class="style1"><textarea name="t7" cols="30" rows="10" id="t7">gifts
red gifts</textarea></td>
      <td class="style1"><textarea name="t8" cols="30" rows="10" id="t8">baskets
red baskets</textarea></td>
    </tr>
    <tr>
      <td class="style1"><p>Here is what should appear </p>
      </td>
      <td class="style1">Here is what should appear:</td>
      <td class="style1">Here is what should appear:</td>
      <td class="style1">Here is what should appear:</td>
    </tr>
  </table>
  <label><br />
  </label>
</form>
</body>
</html>
0
Comment
Question by:ray-solomon
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
Comment Utility
Hi,

I would go with something like this:

if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['field1']) && !empty($_POST['r2']) && !empty($_POST['r3']) && !empty($_POST['r4']) && !empty($_POST['replacethis'])) {
  $source = $_POST['field1'];
  $needle = $_POST['replacethis'];
  $alt1     = $_POST['r2'];
  $alt2     = $_POST['r3'];
  $alt3     = $_POST['r4'];

  $results = array(1 => '', 2 => '', 3 => '');
 
  $lines = preg_split("/[\n\r]+/", $source);
  foreach($lines as $line) {
     if(strstr($line, $needle)) {
        $results[1] .= str_replace($needle, $alt1, $line)."\n";
        $results[2] .= str_replace($needle, $alt2, $line)."\n";
        $results[3] .= str_replace($needle, $alt3, $line)."\n";
     }
  }

 echo '<textarea name="results1" rows="5" cols="40">'.$results[1].'</textarea>';
 echo '<textarea name="results2" rows="5" cols="40">>'.$results[2].'</textarea>';
 echo '<textarea name="results3" rows="5" cols="40">>'.$results[3].'</textarea>';
}

?>

-r-
0
 
LVL 9

Expert Comment

by:tolgaong
Comment Utility

if (isset($_POST["replacethis"])){
      $textarea="";
      $texts=$_POST["field1"];
      $replacestr=$_POST["replacethis"];
      preg_match_all('/(.*?'.$replacestr.'.*?)/', $texts, $result, PREG_PATTERN_ORDER);
      for ($i = 0; $i < count($result[0]); $i++) {
            $textarea.=$result[0][$i]."\r\n";
            
            }
      $alternates=array($_POST["r2"],$_POST["r3"],$_POST["r4"]);
      foreach($alternates as $val){
            $result = preg_replace('/'.$replacestr.'/', $val, $textarea);
            echo nl2br($result);
            }
      }
0
 
LVL 10

Author Comment

by:ray-solomon
Comment Utility
It's very late I will test this in the morning.
0
 
LVL 10

Author Comment

by:ray-solomon
Comment Utility
Thanks Roonaan, This was the part I was not understanding and now I get it. Much appriciated.

$results = array(1 => '', 2 => '', 3 => '');
 
  $lines = preg_split("/[\n\r]+/", $source);
  foreach($lines as $line) {
     if(strstr($line, $needle)) {
        $results[1] .= str_replace($needle, $alt1, $line)."\n";
        $results[2] .= str_replace($needle, $alt2, $line)."\n";
        $results[3] .= str_replace($needle, $alt3, $line)."\n";
     }
  }
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

728 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

8 Experts available now in Live!

Get 1:1 Help Now