Solved

Replace words in textboxes with conditions

Posted on 2006-07-12
4
245 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
[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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 count occurrences of each item in an array.

726 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