Solved

Replace words in textboxes with conditions

Posted on 2006-07-12
4
236 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
ID: 17096698
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
ID: 17097419

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
ID: 17097562
It's very late I will test this in the morning.
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17102604
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can't find PHP files on account that has WordPress 3 27
send post values 7 26
how to use 2 SELECT statements with prepared statements 19 30
PHP Form Calculate Total Price 10 42
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
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.

773 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